<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      Day 86 VUE——Vuex

      Vuex

      Vuex 是一個專為 Vue.js 應用程序開發的狀態管理模式。它采用集中式存儲管理應用的所有組件的狀態,并以相應的規則保證狀態以一種可預測的方式發生變化。Vuex 也集成到 Vue 的官方調試工具 devtools extension,提供了諸如零配置的 time-travel 調試、狀態快照導入導出等高級調試功能。

      Vuex的基本使用

      第一步:vue create test_vuex

      第二步:選擇含有 vue-router 與 vuex 的模板或者新建模板,注意一定要加 babel 

      第三步:cd test_vuex & npm run serve

      import Vue from 'vue'
      import App from './App.vue'
      import router from './router'
      
      // 導入創建的 store
      import store from './store'
      
      Vue.config.productionTip = false
      
      new Vue({
        router,
      
        // 一定要掛載
        store,
        render: h => h(App)
      }).$mount('#app')
      main.js
      <template>
        <div id="app">
          <div id="nav">
            <router-link to="/">Home</router-link> |
            <router-link to="/about">About</router-link> | 
            {{ $store.state.count }}
          </div>
          <router-view/>
        </div>
      </template>
      
      <style>
      #app {
        font-family: Avenir, Helvetica, Arial, sans-serif;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
        text-align: center;
        color: #2c3e50;
      }
      
      #nav {
        padding: 30px;
      }
      
      #nav a {
        font-weight: bold;
        color: #2c3e50;
      }
      
      #nav a.router-link-exact-active {
        color: #42b983;
      }
      </style>
      App.vue
      import Vue from 'vue'
      // 導入模塊
      import Vuex from 'vuex'
      
      // 使用當前插件
      Vue.use(Vuex)
      
      // 創建 store
      export default new Vuex.Store({
        state: {  // 當前的狀態
          count:0
        },
        mutations: {  // 聲明同步的方法
          plus(state){
            // 修改狀態
            state.count++
          },
          reduce(state){
            state.count--
          }
        },  
        // 異步
        // actions: {  // 聲明異步的方法
        //   // commit mutations 中聲明的方法
        //   plus({commit}){
        //     commit('plus')
        //   },
        //   reduce({commit}){
        //     commit('reduce')
        //   }
        // },
        modules: {
        }
      })
      store>index.js
      <template>
        <div class="about">
          <h1>This is an about page</h1>
          <button @click="plus">+1</button>
          <h2>{{ count }}</h2>
          <button @click="reduce">-1</button>
        </div>
      </template>
      
      <script>
      export default {
        computed:{
          count(){
            return this.$store.state.count;
          }
        },
        methods:{
          plus(){
            // dispatch 觸發 actions 中聲明的方法(異步)
            // this.$store.dispatch('plus')
            // 同步
            this.$store.commit('plus')
      
          },
          reduce(){
            // 異步
            // this.$store.dispatch('reduce')
            // 同步
            this.$store.commit('reduce')
          }
        }
      
      }
      </script>
      views>about.vue

       

      為什么要使用actions

      一旦牽扯到異步 一定要使用 actions 方法。不然界面顯示與后臺獲取的數據會不一致

      import Vue from 'vue'
      // 導入模塊
      import Vuex from 'vuex'
      
      // 使用當前插件
      Vue.use(Vuex)
      
      // 創建 store
      export default new Vuex.Store({
        state: {  // 當前的狀態
          count:0
        },
        mutations: {  // 聲明同步的方法
          plus(state){
            // 修改狀態
            state.count++
          },
          reduce(state){
            state.count--
          },
          plusAsync(state){
            state.count++
          }
        },  
        // 異步
        actions: {  // 聲明異步的方法
        //   // commit mutations 中聲明的方法
        //   plus({commit}){
        //     commit('plus')
        //   },
        //   reduce({commit}){
        //     commit('reduce')
        //   }
        plusAsync({commit}){
          commit('plusAsync')
        }
        },
        modules: {
        }
      })
      index.js
      <template>
        <div class="about">
          <h1>This is an about page</h1>
          <button @click="plus">+1</button>
          <h2>{{ count }}</h2>
          <button @click="reduce">-1</button>
          <button @click="plusAsync">+1 異步</button>
        </div>
      </template>
      
      <script>
      export default {
        computed:{
          count(){
            return this.$store.state.count;
          }
        },
        methods:{
          plus(){
            // dispatch 觸發 actions 中聲明的方法(異步)
            // this.$store.dispatch('plus')
            // 同步
            this.$store.commit('plus')
      
          },
          reduce(){
            // 異步
            // this.$store.dispatch('reduce')
            // 同步
            this.$store.commit('reduce')
          },
          plusAsync(){
            this.$store.commit('plusAsync')
          }
        }
      
      }
      </script>
      About.vue

       

      Vuex系列的輔助函數的運用

      import { mapState,mapGetters,mapMutations,mapActions } from 'vuex'
       
      import Vue from 'vue'
      // 導入模塊
      import Vuex from 'vuex'
      
      // 使用當前插件
      Vue.use(Vuex)
      
      // 創建 store
      export default new Vuex.Store({
        state: {  // 當前的狀態
          count:0,
          username:'亦雙弓'
        },
        getters:{
          odd_even(state){
            return state.count % 2 === 0 ? '偶數' : '奇數'
          }
        },
        mutations: {  // 聲明同步的方法
          plus(state){
            // 修改狀態
            state.count++
          },
          reduce(state){
            state.count--
          },
          plusAsync(state){
            state.count++
          }
        },  
        // 異步
        actions: {  // 聲明異步的方法
        //   // commit mutations 中聲明的方法
        //   plus({commit}){
        //     commit('plus')
        //   },
        //   reduce({commit}){
        //     commit('reduce')
        //   }
        plusAsync({commit}){
          commit('plusAsync')
        }
        },
        modules: {
        }
      })
      index.js
      <template>
        <div class="about">
          <h1>This is an about page</h1>
          <button @click="plus">+1</button>
          <h2>{{ myCount }} --- {{ odd_even }} --- {{ user }}</h2>
          <button @click="reduce">-1</button>
          <button @click="plusAsync">+1 異步</button>
        </div>
      </template>
      
      <script>
      import { mapState,mapGetters,mapMutations,mapActions } from 'vuex'
      export default {
        computed:{
          // count(){
          //   return this.$store.state.count;
          // }
          // odd_even(){
          //   return this.$store.getters.odd_even;
          // },
          // mapState 的使用
          // ...mapState(['count'])
          ...mapState({
            myCount:'count',
            user:'username'
          }),
          // mapGetters 的使用
          ...mapGetters(['odd_even'])
        },
        methods:{
          // plus(){
          //   // dispatch 觸發 actions 中聲明的方法(異步)
          //   // this.$store.dispatch('plus')
          //   // 同步
          //   this.$store.dispatch('plus')
      
          // },
      
          // 簡寫 mapActions 的使用
          ...mapActions(['plus']),
      
      
          // reduce(){
          //   // 異步
          //   // this.$store.dispatch('reduce')
          //   // 同步
          //   this.$store.commit('reduce')
          // },
          // plusAsync(){
          //   this.$store.commit('plusAsync')
          // }
      
          // 簡寫 mapMutations 的使用
          ...mapMutations(['reduce','plusAsync'])
        }
      
      }
      </script>
      About.vue

       

      如何在組件內部提交數據給vuex

      <template>
        <div class="about">
          <h1>This is an about page</h1>
          <button @click="plus">+1</button>
          <h2>{{ myCount }} --- {{ odd_even }} --- {{ user }}</h2>
          <button @click="reduce">-1</button>
          <button @click="plusAsync">+1 異步</button>
        </div>
      </template>
      
      <script>
      import { mapState,mapGetters,mapMutations,mapActions } from 'vuex'
      export default {
        computed:{
          ...mapState({
            myCount:'count',
            user:'username'
          }),
          ...mapGetters(['odd_even'])
        },
        methods:{
          ...mapActions(['plus']),
      
          // 在組件內部提交數據  載荷形式分發
          plusAsync(){
            this.$store.dispatch('plusAsync',{
              amount:10
            })
          },
      
          // 簡寫 mapMutations 的使用
          // ...mapMutations(['reduce','plusAsync'])
          ...mapMutations(['reduce'])
        }
      }
      </script>
      About.vue
      import Vue from 'vue'
      // 導入模塊
      import Vuex from 'vuex'
      
      // 使用當前插件
      Vue.use(Vuex)
      
      // 創建 store
      export default new Vuex.Store({
        state: {  // 當前的狀態
          count: 0,
          username: '亦雙弓'
        },
        getters: {
          odd_even(state) {
            return state.count % 2 === 0 ? '偶數' : '奇數'
          }
        },
        mutations: {  // 聲明同步的方法
          plus(state) {
            // 修改狀態
            state.count++
          },
          reduce(state) {
            state.count--
          },
          // 接收值
          plusAsync(state, amount) {
            state.count += amount
          }
        },
        // 異步
        actions: {  // 聲明異步的方法
          // 傳入值
          plusAsync({ commit }, { amount }) {
            console.log(amount);
            setTimeout(() => {
              commit('plusAsync', amount)
            }, 1000);
          }
        },
        modules: {
        }
      })
      index.js

       

      posted @ 2020-08-23 02:25  亦雙弓  閱讀(128)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 国产精品女同一区三区五区| 伊人久久大香线蕉av色婷婷色| 高清有码国产一区二区| 无码中文字幕热热久久| 性色在线视频精品| 免费无码一区无码东京热| 欧美亚洲精品中文字幕乱码 | 综合色一色综合久久网| 99人体免费视频| 真人性囗交视频| 午夜大尺度福利视频一区| 亚洲av日韩av中文高清性色| 国产精品一二三区久久狼| 国产av国片精品一区二区| 久久一本人碰碰人碰| 亚洲久久色成人一二三区| 河北区| 中文字幕人妻有码久视频| 亚洲欧洲日韩国内精品| 国内精品久久久久影院网站 | 国产中文字幕精品免费| 极品人妻少妇一区二区三区| 亚洲成色精品一二三区| 国产在线精彩自拍视频| 亚洲AV毛片一区二区三区| 中文熟妇人妻av在线| 视频一区视频二区在线视频| 大陆熟妇丰满多毛xxxⅹ| 国产精品久久久久久久久鸭| 无码人妻斩一区二区三区| 中文字幕有码无码AV| 日本真人添下面视频免费| 老司机性色福利精品视频| 中文字幕日韩精品人妻| 午夜天堂av天堂久久久| 亚洲精品成人无限看| 国内精品一区二区不卡| a∨变态另类天堂无码专区| 九九热在线免费视频播放| 久久久无码精品亚洲日韩按摩| 国产精品中文字幕视频|