亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

淺談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傳值問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論