淺談vuex的基本用法和mapaction傳值問題
更新時間:2019年11月08日 10:41:13 作者:請不要讓我脫發(fā)
今天小編就為大家分享一篇淺談vuex的基本用法和mapaction傳值問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
vuex的理論知識就不多提了,官網上已經有明確的講解。
用一個簡單的例子來描述一下基本的用法:
第一步:npm install vuex –save-dev
第二步:在目錄中創(chuàng)建store目錄配置管理狀態(tài)
//store/index.js /** * Created by zhaohuan on 2017/7/13. */ import Vue from 'vue' import Vuex from 'vuex'; Vue.use(Vuex); const store = new Vuex.Store({ state: { msg: '原始數(shù)據(jù)' }, actions: { fun: function ({commit},date) { commit('saveAdminInfo', {list: date}); }, }, mutations: { saveAdminInfo(state, adminInfo){ state.msg = adminInfo.list; } } }); export default store;
第三步:在main.js中引入store
new Vue({ el: '#app', router, store, template: '<App/>', components: { App } });
第四部:編寫路由頁面
//test1.vue <template> <div> msg:{{msg}} <input type="text" v-model="checkedNames" style="border: 1px solid red"> <button @click="fun">提交</button> </div> </template> <script> import {mapState,mapActions} from 'vuex'; export default{ data(){ return{ checkedNames:'' } } , computed: {...mapState(['msg'])}, methods: { // ...mapActions(['fun']); //如果需要向actions里面?zhèn)髦稻褪謩诱{用 fun(){ this.$store.dispatch('fun',this.checkedNames); } //...mapActions(['fun'])== this.$store.dispatch('fun'); } } </script>
test2.vue
<template> <div> msg:{{msg}} </div> </template> <script> import {mapState} from 'vuex'; export default{ computed: {...mapState(['msg'])}, //對應getters.技術中的msg // methods: {...mapActions(['fun'])} } </script>
修改之前:
test1
test2
以上這篇淺談vuex的基本用法和mapaction傳值問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue3的watch用法以及和vue2中watch的區(qū)別
這篇文章主要介紹了vue3的watch用法以及和vue2中watch的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04vue webpack build資源相對路徑的問題及解決方法
這篇文章主要介紹了vue webpack build資源相對路徑的問題,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06vue3中使用viewerjs實現(xiàn)圖片預覽效果的項目實踐
viewer.js是一款開源的圖片預覽插件,功能十分強大,本文主要介紹了vue3中使用viewerjs實現(xiàn)圖片預覽效果的項目實踐,具有一定的參考價值,感興趣的可以了解一下2023-09-09element修改form的el-input寬度,el-select寬度的方法實現(xiàn)
有時候像form表單這樣,頁面的input、select等寬度不一定會是一樣的,可能有些長,有些短,本文就介紹了如何element修改form的el-input寬度,el-select寬度的方法實現(xiàn),感興趣的可以了解一下2022-02-02