vuex模块化使用

module/user.js代码

```

import Vue from 'vue'

import Vuex from 'vuex'

Vue.use(Vuex)

const state = {

 liu:'jingna',

 wei:['yu','ning']

}

const mutations = {

 changeName(state,res){

     state.liu = res

 }

}

const actions = {

 changeNameA(context){

     context.commit('changeName','南京')

 }

}

export default {

 state,mutations,actions

}

```

index.js代码

```

```

import Vue from 'vue'

import Vuex from 'vuex'

import user from "./module/user"

Vue.use(Vuex)

export default new Vuex.Store({

 modules:{

user,

 },

  state: {

        count:7777

  },

  mutations: {

    change(state, value){

      // eslint-disable-next-line no-console

      console.log(value)

      // eslint-disable-next-line no-undef

      state.count=value

    },

  },

  actions: {

  changevalue(context,params){

    context.commit("change",params.count)

  }

  }

})

vue组件中使用

``

`<template>

  <div class="about">

    <p>{{count}}</p>

    <p>{{username}}</p>

    <button @click="add">点击</button>

    <button @click="namec">改名字</button>

    <headertop></headertop>

  </div>

</template>

<script>

import headertop from "@/components/header";

import { mapActions, mapState } from "vuex";

export default {

  name: "about",

  data() {

    return {};

  },

  computed: {

    count: {

      get() {

        return this.$store.state.count;

      }

    },

    username: {

      get() {

        return this.$store.state.user.liu;

      }

    }

  },

  components: {

    headertop

  },

  methods: {

    add() {

      this.$store.dispatch("changevalue", { count: 25 });

    },

    namec() {

      this.$store.commit("changeName", "深圳");

    }

  }

};

</script>

```

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 安装 npm npm install vuex --save 在一个模块化的打包系统中,您必须显式地通过Vue.u...
    萧玄辞阅读 3,075评论 0 7
  • vuex 场景重现:一个用户在注册页面注册了手机号码,跳转到登录页面也想拿到这个手机号码,你可以通过vue的组件化...
    sunny519111阅读 8,179评论 4 111
  • 姓名:岳沁 学号:17101223458 转载自:http://blog.csdn.net/h5_queensty...
    丘之心阅读 2,227评论 0 1
  • Vuex 的学习记录 资料参考网址Vuex中文官网Vuex项目结构示例 -- 购物车Vuex 通俗版教程Nuxt....
    流云012阅读 1,568评论 0 7
  • actions 所有异步操作只能放在actions 中 Action 可以包含任意异步操作。(为了告诉自己的,怕忘...
    城门小胡同阅读 418评论 0 1

友情链接更多精彩内容