Vuex全局变量存储调用

news/2024/7/19 13:04:30 标签: js, svg, vue.js

1.入口文件main.js

import Vue from 'vue'
import Layout from './layout'
import router from './router'
// 引入vuex
import store from './store/store

vue实例中


/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { Layout },
  template: '<Layout/>'
})

store文件下创建store.js

 store.js中设置

import Vue from 'vue'
import Vuex from 'vuex'
 
Vue.use(Vuex)
 
const state = {
  pjtnews: 0,
  count: 1
}
 
const mutations = {
  add(state) {
    state.count += 1;
  },
  reduce(state) {
    state.count -= 1;
  }
}
export default new Vuex.Store({
  state,
  mutations
});

 以上参考 :vue项目中如何引入vuex__cris的博客-CSDN博客_vuex引入

————————————     分割     ——————————————

存储全局变量

import { mapActions, mapGetters } from 'vuex'

this.$store.commit("hotline/setUserInfo", this.hotLineData);
//hotline: 是src/store文件的modules中的js文件 , this.hotLineData是该文件中定义的函数
//即第一个参数为方法,第二个参数为数据

 src/store/modules/hotline.js文件如下,全局数据通过调用mutations中的方法存放在state对象中,

  /**
   * 存放 ** 数据
   * **/
  
  // initial state
  const state = {
    userInfo: [],
    nowInfo: {}
  }
  
  // getters
  const getters = {}
  
  // actions
  const actions = {}
  
  // mutations
  const mutations = {
    setUserInfo(state, userInfo) { //设置参数
      state.userInfo = userInfo;
    },
    setNowInfo(state, nowInfo) { //设置参数
      state.nowInfo = nowInfo;
    }
  }
  
  export default {
    namespaced: true,
    state,
    getters,
    actions,
    mutations
  }

全局变量的拿取使用:

在其他vue文件的计算属性下拿到,就可直接this.userInfo  、this.nowInfo,调用了

import { mapState, mapActions } from "vuex"


computed: {
    ...mapState({
      userInfo: (state) => state.hotline.userInfo,
      nowInfo: (state) => state.hotline.nowInfo,
    }),
  },

Vuex官方文档:开始 | Vuex


http://www.niftyadmin.cn/n/1653322.html

相关文章

wp文件转shp_MapGIS完美转shp攻略

完美实现从mapGIS文件转换成ArcGIS中shp格式文件。一、直接替换DBF文件&#xff0c;步骤如下1.单独把要转换的mapGIS文件的属性用mapGIS软件中的“属性库管理”功能模块导出成“*.dbf”格式&#xff1b;2.用mapGIS软件中的“文件转换”功能模块把原mapGIS文件转换成shp格式&…

vue项目跨域问题,解决方法等同于nginx

时隔一个月&#xff0c;又遇到了跨域问题 //screenMap.vueaxios({method: "GET",url: "api/jhzhps-back/task/sta/statistics",// data: obj,headers: {login_token:"eyJ0eXiOiJV1QLCJhbGiJ9.eyJleHAiO",},}).then(function (res) {console.log…

mysql 5.6 dump_mysql5.6 的--dump-slave参数的用法

,使用该参数&#xff0c;我们可以在slave节点导出数据用于建立新的slave&#xff0c;避免对主库的压力。方法如下&#xff1a;在slave上执行&#xff1b;1、查看当前同步的状态# show slave status\G 主要关注下面的部分&#xff1a;Master_Log_File: mysql-bin.000095Read_Ma…

Failed to prettify component 。。screenMap.vue template source after compilation.

vue项目运行&#xff0c;出警告&#xff0c;项目可以正常启动 通过反复折叠代码&#xff0c;运行项目&#xff0c;警告就消失了 预计是代码格式的问题。

Day1作业: 登录接口和多级菜单

1、登录接口实现功能&#xff1a;1用户注册2用户登录3用户名是否存在4密码错误三次锁定&#xff0c;并更新用户锁定状态5用户锁定状态匹配流程图涉及到1个文件作为库passwd #用户密码文件&#xff0c;1为锁定状态zhy:123456:0alex:234567:0xiaoming:45678:1 python代码1 #…

nginx常用命令,跨域问题

在跨域问题上&#xff0c;nginx特别好用&#xff0c;本人常用的命令有 //cd到nginx文件下&#xff08;即与nginx应用文件同级&#xff09;&#xff0c;或该文件下运行cmd start nginx //启动nginx nginx -s reload //重加载nginx文件 nginx -s stop //强制停止…

python嵌套字典赋值_实现嵌套字典的最佳方法是什么?

在Python中实现嵌套字典的最佳方法是什么&#xff1f;实施__missing__在.上dict类来设置和返回一个新实例。这种方法是可用的。(并记录在案)自从Python2.5之后&#xff0c;和(对我来说特别有价值)很漂亮的指纹就像个普通的白痴&#xff0c;而不是丑陋的打印一个自动形象的默认&…

vue项目添加router(最基础版本)

安装依赖 npm install vue-router /src/router/index.js /src/main.js /src/App.vue