在vue中使用vuex状态管理工具进行传值:
首先安装vuex
使用npm:
npm install vuex --save-dev
第一步在文件中创建如下文件夹
第二步:需要在项目main.js文件中引入store
import store from './store/index'
省略代码.....
new Vue({
el: '#app',
router,
store,
data:{},
components: { App },
template: '<App/>'
})
第三步:在在store的index.js中添加如下代码:
import Vue from 'vue'
import Vuex from 'vuex'
import logIn from './modules/logln'
Vue.use(Vuex)
const state ={}
const actions={}
const mutations={}
const store = new Vuex.Store({
modules:{
logIn
},
actions,
state,
mutations
})
export default store
Vue.use(Vuex);
在logIn.js中写入需要的值:
const state ={
session_id: "",
}
const getters ={}
const actions = {}
const mutations={
getSession_id(state,value){
state.session_id = value;
},
}
export default {
namespaced:true,
state,
getters,
actions,
mutations
}
之后就是页面使用了
在需要传值得地方使用:
dianji(){
console.log('123')
this.)
}
在需要使用的地方
session_id(){
return this.$store.state.logIn.session_id;
},