首页 > 其他 > 详细

vuex的使用

时间:2017-08-12 14:29:44      阅读:188      评论:0      收藏:0      [点我收藏+]

1.先安装npm install vuex --save-dev 

2.在src下建立文件夹vuex,在vuex里面写store.js

/**
 * Created by ywz on 2017/8/12.
 */
import Vue from ‘vue‘
import Vuex from ‘vuex‘

Vue.use(Vuex)

const store = new Vuex.Store({
  // 定义状态
  state: {
    author: ‘Wise Wrong‘
  },
  mutations:{
    newAuthor (state,msg){
      state.author=msg;
    }
  }
})

export default store

第三步 在main.js中
import Vuex from ‘vuex‘
Vue.use(Vuex);
/* eslint-disable no-new */
new Vue({
el: ‘#app‘,
router,
store,
render: h => h(App)
});

第四部显示在页面
<div>自拍照{{author}}</div>

computed: {
author()
{
return this.$store.state.author;
}
}

第5步 设置修改
methods: {
setAuthor(){
//this.$store.state.author=this.user;
this.$store.commit(‘newAuthor‘,32223);
}
},

vuex的使用

原文:http://www.cnblogs.com/norm/p/7350024.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!