zoukankan      html  css  js  c++  java
  • vuex 的基本使用

    工程目录

    主要关注store 文件夹下的文件

    store/index.js

    import Vue from 'vue'
    import Vuex from 'vuex'
    // import getters from './store/getters.js'
    // import actions from './store/actions.js'
    // import mutations from './store/mutations.js'
    import types from './types'
    
    // children module
    import users from './modules/users.js'
    
    Vue.use(Vuex)
    
    const state = {
      count: 1
    }
    
    const mutations = {
      [types.INCREMENT]: (state, n) => {
        state.count = state.count + n
      },
      [types.DECREMENT]: (state, n) => {
        state.count = state.count - n
      }
    }
    
    const actions = {
      increment: (context, n = 1) => {
        context.commit(types.INCREMENT, n)
      },
      decrement: (context, commit, n = 1) => {
        context.commit(types.DECREMENT, n)
      }
    }
    
    export default new Vuex.Store({
      state,
      mutations,
      actions,
      modules: {
        users
      }
    })

    store/modules/users.js

    import types from '../types'
    
    const state = {
      username: 'xiaojf'
    }
    
    const mutations = {
      [types.CHANGEUSERNAME]: (state, username) => {
        state.username = username
      }
    }
    
    const actions = {
      changeUsername (context, username = 'zhangsan') {
        context.commit(types.CHANGEUSERNAME, username)
      }
    }
    
    export default {
      state,
      mutations,
      actions
    }

    /components/test.vue

    <template>
      <div>
        <div class="test">{{name}}</div>
        <div class="test">{{this.$store.state.count}}</div>
        <button v-on:click="increment()">increment</button>
        <button v-on:click="decrement()">decrement</button>
    
        <br>
        this is users module state <span style="color: red;" v-on:click="changeUsername()"> {{this.$store.state.users.username}}</span>
    
        <br>
        <test2></test2>
      </div>
    </template>
    
    <script>
    import test2 from './test2'
    
    export default {
      name: 'test',
      data: function () {
        return {
          name: 'xiaojf'
        }
      },
      components: {
        test2
      },
      methods: {
        increment () {
          // mutation
          this.$store.dispatch('increment', 1)
        },
        decrement () {
          // action
          this.$store.dispatch('decrement', 2)
        },
        changeUsername () {
          // children module's action
          this.$store.dispatch('changeUsername', 'xiaojianfeng')
        }
      }
    }
    </script>
    
    <style scoped>
      .test {
        font-size:  28px;
        color: red;
      }
    </style>

    /components/test.vue

    <template>
      <div>
        this is test2 {{this.$store.state.count}}
      </div>
    </template>
    
    <script>
    export default {
      name: 'test2',
      data: function () {
        return {
          name: 'xiaojf'
        }
      }
    }
    </script>
  • 相关阅读:
    微服务迁移记(七):使用docker发布 springcloud应用
    intelliJ IDEA docker部署springboot项目
    docker部署应用时超时解决
    centos下docker安装
    freemarker自定义分页(springboot、bootstrap4)
    微服务迁移记(六):集成jwt保护微服务接口安全
    微服务迁移记(五):WEB层搭建(5)-集成ueditor编辑器,伪分布式图片上传
    微服务迁移记(五):WEB层搭建(4)-简单的权限管理
    微服务迁移记(五):WEB层搭建(3)-FreeMarker集成
    WebView使用input file控件打开相册上传图片
  • 原文地址:https://www.cnblogs.com/xiaojf/p/11359361.html
Copyright © 2011-2022 走看看