vuex的简单使用

项目结构

一、创建store

创建分割成模块(module)的state

项目代码中创建store文件夹,同时创建index.js,在modules中定义userInfo这个state

index.js

import Vue from 'vue'
import vuex from 'vuex'
Vue.use(vuex);

import userInfo from './modules/userInfo.js';//引入某个store对象


//store对象模块
export default new vuex.Store({
  modules: {
    userInfo: userInfo,//用户组件
  }
})

在store文件夹创建moudle文件夹,用于存放不同模块的state,下面创建userInfo.js这个state,state里面可以定义属于该state的getter、actions、mutations等函数

userInfo.js

// initial state
const state = {
  userId:'',//用户id
  userName:'',//用户名称
  token:'',//token
  permission:''//权限
}

// getters
const  getters = {
  // 获取用户信息
  getUserInfo(){
    return state;
  }
}

// actions
const actions = {}

// mutations
const mutations = {
  updateUserInfo(state,payload) {
    state = payload;
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}

二、把创建store引入到main.js中

先导入store

import store from './store/index' //引入store

完整代码

main.js

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import Element from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import App from './App'
import router from './router'
import store from './store/index' //引入store

Vue.config.productionTip = false

Vue.use(Element, { size: 'small', zIndex: 3000 })

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在手机端,使用Vuex可以方便地管理应用程序的状态。Vuex是一个专为Vue.js应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态的一致性。 在手机端使用Vuex,首先需要安装Vuex库。可以通过npm或者yarn进行安装。安装完成后,在Vue项目中创建一个store文件夹,并在该文件夹下创建一个index.js文件。 在index.js文件中,需要引入Vue和Vuex,并创建一个新的Vuex.Store实例。在该实例中,可以定义state、mutations、actions和getters等属性和方法。 state:用于存储应用程序的状态数据。 mutations:用于修改state中的数据。只能进行同步操作。 actions:用于处理异步操作,可以包含多个mutations的调用。 getters:用于获取state中的数据,类似于计算属性。 下面是一个简单的示例: ```javascript // index.js import Vue from 'vue'; import Vuex from 'vuex'; Vue.use(Vuex); const store = new Vuex.Store({ state: { count: 0 }, mutations: { increment(state) { state.count++; }, decrement(state) { state.count--; } }, actions: { incrementAsync(context) { setTimeout(() => { context.commit('increment'); }, 1000); } }, getters: { doubleCount(state) { return state.count * 2; } } }); export default store; ``` 在Vue组件中,可以通过this.$store来访问Vuex的状态和方法。例如: ```javascript // App.vue <template> <div> <p>Count: {{ $store.state.count }}</p> <p>Double Count: {{ $store.getters.doubleCount }}</p> <button @click="$store.commit('increment')">Increment</button> <button @click="$store.dispatch('incrementAsync')">Increment Async</button> </div> </template> <script> export default { name: 'App', mounted() { console.log(this.$store.state.count); } } </script> ``` 以上是手机端使用Vuex简单介绍,希望对你有帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值