vuex的方法:
开启持久化插件:
1.引入:import VuexPersistence from 'vuex-persist'
plugins: [
new VuexPersistence({
storage: window.localStorage
}).plugin
]
2.下载:cnpm i vuex-persist --save
同步转异步
async 方法名({commit}) {
//获取json数据
let {data} = await axios.get('data.json')
//将数据传到stare中
commit('方法名', data)
}
注:{}为变量解构赋值方法
常规写法中的 context = { dispatch: local.dispatch,
commit: local.commit,
getters: local.getters,
state: local.state,
rootGetters: store.getters,
rootState: store.state }
使用 变量解构赋值后 { commit } 的 commit= context.commit 了。
路由方法:
1.路由重定向:redirect:"/home"
map方法:
import { mapState, mapMutations, mapActions } from "vuex";//引入
//在函数中调用
...mapState(["数据名"]),
...mapMutations(["方法名"]),
...mapActions(["方法名"]),
使用this.调用引入过来的vuex方法
跳转:
this.$router.push("路径")
组件:
//将子组件中的方法暴露到父组件中
@click="$store.commit('delect')"