//store.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
state: {...},
mutations: {...},
actions: {...}
})
export default store
//main.js
...
import store from './store'
Vue.prototype.$store = store
const app = new Vue({
store,...
})
...
//test.vue 使用时:
import {mapState,mapMutations} from 'vuex'
复制代码
<!--state改变不会实时渲染-->
export default {
data() {
return {
name:this.$store.state.name,
};
},
}
复制代码
<!--监听state改变从新渲染视图-->
<template>
<div>
{{name}}
</div>
<template>
export default {
computed: {
name() {
return this.$store.state.name;
}
},
}
复制代码
注意: data中的变量若是和computed中的变量重名,data优先,注意命名vue
<!--取多个很冗余,繁琐-->
export default {
computed: {
token(){
return this.$store.state.token;
},
name(){
return this.$store.state.name;
}
},
}
复制代码
<!--mapState 直接取出-->
import { mapState } from 'vuex'
export default {
computed: mapState([
'token',
'name'
])
}
复制代码
import { mapState } from 'vuex'
export default {
computed:{
getTime(){
return 123;
},
...mapState([
'token',
'name'
])
}
}
复制代码
<template>
<div>
xiaokeai,dahuilang是咱们取的别名
token,name是state的值
{{xiaokeai}}
</div>
<template>
<script>
import { mapState } from 'vuex'
export default {
computed:{
...mapState({
xiaokeai:"token",
dahuilang:"name",
})
}
}
</script>
复制代码
<template>
<div>
假如token的值123;
balabala的值就是 123皮卡皮
{{balabala}}
</div>
<template>
<script>
import { mapState } from 'vuex'
export default {
data(){
return {
pikaqiu:"皮卡皮卡"
}
}
computed:{
...mapState({
xiaokeai:"token",
dahuilang:"name",
// 为了可以使用 `this` 获取局部状态,使用一个自定义名字的函数
balabala(state){
return state.token + this.pikaqiu;
}
})
}
}
</script>
复制代码
<template>
<div>
{{daSon}}
{{xiaoSon}}
</div>
</template>
<script>
import { mapState } from 'vuex'
export default {
data(){
return {
pikaqiu:"皮卡皮卡"
}
}
computed:{
...mapState({
daSon: state=>state.obj.yeye.baba.daSon,
xiaoSon:state=>state.obj.yeye.baba.xiaoSon,
})
}
}
</script>
复制代码
这种方式取对象写到业务里不直观,也不共用,下节==Getter==有更优的方案vuex
能够 this.$store.getters.xxx 使用,也可使用mapGetters辅助函数,==辅助函数注意:== 和state同样,注入在computed中浏览器
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
const store = new Vuex.Store({
state: {
obj: {
yeye: {
baba: {
daSon: "老大",
xiaoSon: "老二"
}
}
}
},
getters: {
<!--将想要提取或者处理的值这里处理好-->
getson: state => {
return state.obj.yeye.baba;
}
}
})
export default store
<!--用的时候,和state同样,也能够别名等等操做-->
<template>
<div>
{{getson}}
</div>
</template>
<script>
import { mapGetters } from 'vuex'
export default {
computed: {
...mapGetters([
getson
])
}
}
</script>
复制代码
操做: this.$store.commit("名字","值");bash
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
const store = new Vuex.Store({
state: {
token: "vuex的token",
},
mutations: {
setToken(state, val) {
state.token = val;
}
}
})
export default store
复制代码
mapMutations 辅助函数,和state同样,能够别名, ==注意:== 辅助函数注入在methods中session
methods: {
...mapMutations([
'setToken'
])
}
<!--使用-->
this.setToken(100); //token修改成100
复制代码
Mutation 必须是同步函数,不要误解这句话,觉得异步不能用,异步能够用在里面,视图的渲染,取值都没有问题,问题在于:调试的时候,一些浏览器调试插件不能正确监听state。因此方便调试,尽可能将异步写入action中app
注意action的 参数不是 state ,而是context,context里面包含commit,state。基本操做:this.$store.dispatch("函数名","值")异步
const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
increment (state) {
state.count++
}
},
actions: {
increment (context) {
context.commit('increment')
}
}
})
<!--辅助函数操做 注入在methods中-->
import { mapActions } from 'vuex'
export default {
methods: {
...mapActions([
"increment"
])
}
}
<!--使用-->
this.increment(123);
复制代码
module 目的将store按功能拆分红多个文件,利于维护管理,module 分为2种状况,1.是有命名空间, 2.是没有命名空间函数
import Vue from 'vue';
import Vuex from 'vuex';
import moduleA from "./modules/cat.js";
Vue.use(Vuex);
const store = new Vuex.Store({
state: {
token: "vuex的token",
},
modules:{
moduleA
}
})
export default store;
<!--moduleA.js-->
export default {
// namespaced: true,
state: {
cat:"我是cat",
},
mutations: {
setCat(state, val) {
state.cat = val;
}
},
actions: {
},
getters: {
}
};
复制代码
this.$store.state.moduleA.cat
或者
...mapState({
cat:state=>state.moduleA.cat,
}),
不能够(state是局部,不能够这么取):
...mapState([
"cat"
]),
复制代码
和原来同样,由于方法是注册在全局的
this.$store.commit("setCat",666);
或者
...mapMutations([
"setCat"
])
复制代码
<!--moduleA.js 文件-->
export default {
namespaced: true,
state: {
cat:"我是cat",
}
}
复制代码
this.$store.state.moduleA.cat
或者
<!--注意这里:命名空间的名字带上,在modules注册时候呢个key值-->
<!--也能够别名,方法和以前同样,就是第一个参数是空间名-->
...mapState("moduleA",[
"cat"
])
复制代码
<!--只是第一个参数是空间名,其余操做同样-->
...mapMutations("moduleA",[
"setCat"
])
this.setCat(888);
或者:
this.$store.commit("moduleA/setCat",666);
复制代码
vuex页面刷新会丢失数据,用vuex-persistedstate插件可解决ui
import createPersistedState from "vuex-persistedstate";
const store = new Vuex.Store({
state: {},
mutations: {},
actions: {},
getters: {},
modules:{},
plugins: [
createPersistedState({
storage: window.sessionStorage
})
]
})
export default store
复制代码