vuex

Vuex:数据仓库,解决不同组件数据共享,数据持久化。

数据共享也可以用Localstorage,Sessionstorage实现,大型项目适合用vuex
vuex使用

1.在src目录下新建vuex文件夹 2.在vuex文件夹里面新建一个store.js 3.安装cnpm install vuex --save 4.在store.js中引入以下内容

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

5.定义数据

  var state={
    count:1
   }

6.定义方法,moutations里面的方法主要用于改变state里面的数据

  var motations={
    inCount(){
      ++state.count;
    }
   }

7.暴露对象

const store = new Vuex.Store({
  state,
  mutations,
})
export default store

8.组建里面使用

a.引入js文件import store from ‘…/vuex/store.js’
b.在export default中注册store
c使用:this. s t o r e . s t a t e . k e y d . 改 变 数 据 , 在 方 法 中 写 入 以 下 语 句 , 操 作 m u t a t i n s 里 面 的 方 法 t h i s . store.state.key d.改变数据,在方法中写入以下语句,操作mutatins里面的方法 this. store.state.keyd.mutatinsthis.store.commit(‘intCount’)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值