Vuex 相关初始化配置

安装配置vuex

  1. 终端输入:npm i vuex    进行安装
  2. 创建文件  src/store/index.js
  3. 在index.js文件中配置以下内容:
    //引入Vue核心库
    import Vue from 'vue'
    //引入Vuex
    import Vuex from 'vuex'
    //应用Vuex插件
    Vue.use(Vuex)
       
    //准备actions对象——响应组件中用户的动作
    const actions = {}
    //准备mutations对象——修改state中的数据
    const mutations = {}
    //准备state对象——保存具体的数据
    const state = {}
       
    //创建并暴露store
    export default new Vuex.Store({
    	actions,
    	mutations,
    	state
    })
  4. 在main.js中配置以下内容:
    ......
    //引入store
    import store from './store'
    ......
       
    //创建vm
    new Vue({
    	el:'#app',
    	render: h => h(App),
    	store
    })
      
  5. 此时在组件实例身上已经存在$store,已经可以调用:

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值