vue 安装使用 vuex

# vuex 的核心概念

核心概念
State访问状态对象
Getter计算过滤操作
Mutation修改状态
Action异步修改状态
Module模块组


# npm 安装

npm install vuex --save


# 在 src 文件夹下新建 vuex 文件夹,再在 vuex 文件夹下新建 store.js 文件

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);


export default new Vuex.Store({
  state: {  // 全局变量在这里定义并且初始化,获取方法 this.$store.state.name
    name: 'hello',
    todos: [
      { id: 1, text: '...', done: true },
      { id: 2, text: '...', done: false }
    ]
  },
  getters: {  // 全局方法在这里编写  方法名: state => { 方法体 },获取方法 this.$store.getters.getData
    getData: state => {
      return state.todos.filter(todo => todo.done);
    }
  },
  mutations: {  // 这里同步操作全局变量,获取方法 this.$store.commit('setName', {newName: 'hello'})
    setName(state, payload) {
      state.name = payload.newName;
    }
  },
  actions: {  // 这里可以异步操作全局变量,获取方法 this.$store.dispatch('setName', {newName: 'hello'})
    setName({commit}, payload) {
      commit('setName', payload);
    }
  }
});


# 引入 store.js

在 main.js 中写入以下内容:

import Vue from 'vue'
import App from './App'
import router from './router'
import store from './vuex/store.js'

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})


# 页面中使用

<template>
  <div id="app">
    {{$store.state.name}}
    {{$store.getters.getData}}
    <button @click="$store.commit('setName', {newName: 'hello'})">更改name为hello</button>
    <button @click="$store.state.name = '123'">更改name为123</button>
    <button @click="$store.dispatch('setName', {newName: '456'})">更改name为456</button>
    <router-view/>
  </div>
</template>

<script>
export default {
  name: 'App'
}
</script>

<style>
</style>


参考博客:

  1. https://vuex.vuejs.org/zh/installation.html
  2. Vuex教程全集 - 白话篇
  3. 最详细的Vuex教程
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue.js是一个用于构建用户界面的渐进式JavaScript框架,而VuexVue.js官方提供的状态管理库,用于管理Vue.js应用中的数据流。下面是使用Vue 2和Vuex的步骤: 1. 安装Vuex:在项目目录下运行以下命令来安装Vuex: ``` npm install vuex ``` 2. 创建store:在项目中创建一个名为store.js(或者其他你喜欢的名字)的文件,并在其中导入VueVuex: ```javascript import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const store = new Vuex.Store({ // 在这里定义你的状态、mutations、actions等 }) export default store ``` 3. 定义状态:在store.js文件中,你可以定义应用的状态。状态是存储在Vuex中的数据,可以在整个应用中共享和访问。例如,你可以定义一个名为count的状态: ```javascript const store = new Vuex.Store({ state: { count: 0 } }) ``` 4. 定义mutations:mutations是用于修改状态的方法。你可以在mutations中定义一些方法来更新状态。例如,你可以定义一个名为increment的mutation来增加count的值: ```javascript const store = new Vuex.Store({ state: { count: 0 }, mutations: { increment(state) { state.count++ } } }) ``` 5. 使用状态和mutations:在Vue组件中,你可以通过`this.$store.state`来访问状态,通过`this.$store.commit`来调用mutations。例如,在一个组件中,你可以这样使用count状态和increment mutation: ```javascript export default { computed: { count() { return this.$store.state.count } }, methods: { increment() { this.$store.commit('increment') } } } ``` 以上是使用Vue 2和Vuex的基本步骤。当然,Vuex还提供了更多的功能,如actions、getters等,你可以根据具体需求进行学习和使用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值