淺談vuex的基本用法和mapaction傳值問題
vuex的理論知識就不多提了,官網(wǎng)上已經(jīng)有明確的講解。
用一個簡單的例子來描述一下基本的用法:
第一步: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)髦稻褪謩诱{(diào)用
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'])}, //對應(yīng)getters.技術(shù)中的msg
// methods: {...mapActions(['fun'])}
}
</script>
修改之前:
test1

test2

以上這篇淺談vuex的基本用法和mapaction傳值問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue3?使用?vue3-video-play實現(xiàn)在線視頻播放
這篇文章主要介紹了vue3?使用?vue3-video-play?進行在線視頻播放,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06
vue3+TypeScript+vue-router的使用方法
本文詳細講解了vue3+TypeScript+vue-router的使用方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-01-01
vue報錯Error:Cannot?find?module?'fs/promises'的解決方
最近的項目需要用到vue/cli,但是用cnpm安裝vue/cli的時候報錯了,下面這篇文章主要給大家介紹了關(guān)于vue報錯Error:Cannot?find?module?'fs/promises'的解決方式,需要的朋友可以參考下2022-11-11
解決Element中el-date-picker組件不回填的情況
這篇文章主要介紹了解決Element中el-date-picker組件不回填的情況,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
antd?Vue實現(xiàn)Login登錄頁面布局案例詳解?附帶驗證碼驗證功能
這篇文章主要介紹了antd?Vue實現(xiàn)Login登錄頁面布局案例詳解附帶驗證碼驗證功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
vue3組合式api實現(xiàn)v-lazy圖片懶加載的方法實例
vue作為前端主流的3大框架之一,目前在國內(nèi)有著非常廣泛的應(yīng)用,下面這篇文章主要給大家介紹了關(guān)于vue3組合式api實現(xiàn)v-lazy圖片懶加載的相關(guān)資料,需要的朋友可以參考下2022-09-09

