vuex的理論知識就不多提了,官網(wǎng)上已經(jīng)有明確的講解。
創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比漠河網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式漠河網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋漠河地區(qū)。費(fèi)用合理售后完善,10多年實(shí)體公司更值得信賴。
用一個簡單的例子來描述一下基本的用法:
第一步: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" > <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)容了,希望能給大家一個參考,也希望大家多多支持創(chuàng)新互聯(lián)。
名稱欄目:淺談vuex的基本用法和mapaction傳值問題
轉(zhuǎn)載注明:http://jinyejixie.com/article20/jjjsjo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、網(wǎng)站策劃、網(wǎng)站排名、ChatGPT、響應(yīng)式網(wǎng)站、全網(wǎng)營銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)