欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺談vuex的基本用法和mapaction傳值問(wèn)題

 更新時(shí)間:2019年11月08日 10:41:13   作者:請(qǐng)不要讓我脫發(fā)  
今天小編就為大家分享一篇淺談vuex的基本用法和mapaction傳值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

vuex的理論知識(shí)就不多提了,官網(wǎng)上已經(jīng)有明確的講解。

用一個(gè)簡(jiǎn)單的例子來(lái)描述一下基本的用法:

第一步: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 }
});

第四部:編寫(xiě)路由頁(yè)面

//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)髦稻褪謩?dòng)調(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'])}, //對(duì)應(yīng)getters.技術(shù)中的msg
//  methods: {...mapActions(['fun'])}
 }
</script>

修改之前:

test1

test2

以上這篇淺談vuex的基本用法和mapaction傳值問(wèn)題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論