vuex下state数据的存储
来源:互联网 发布:万得数据 编辑:程序博客网 时间:2024/05/29 17:10
1。在src文件下创建一个store文件,里面加入一个store.js文件
。内容大致为
import vuex from 'vuex'import Vue from 'vue'Vue.use(vuex)export default new vuex.Store({ /*state必须*/ state:{ user:{} }, mutations:{ setUser(state,v){ state.user=v; } }})
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
2。在main.js中的vue实例中必须引入,例
import Vue from 'vue'import App from './App'import router from './router'import ElementUI from 'element-ui'import 'element-ui/lib/theme-default/index.css'import * as api from './config'import mycom from './components/index'import store from './store/store'Vue.prototype.$api = apiVue.config.productionTip = falseVue.use(ElementUI)Vue.use(mycom)Vue.test = function () { alert()}/* eslint-disable no-new */new Vue({ el: '#app', router, store:store, template: '<App/>', components: { App }})
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
3。在看在哪里引用setUser方法,例如在login.vue中引用,一般在methods中会用到,如
<template> <div> <el-input v-model="user.username"></el-input> <el-input v-model="user.password"></el-input> <el-input v-model="user.checkcode"></el-input> <img :src="codeurl" alt=""> <!--<el-button type="sucess" @click="loginUser">提交</el-button>--> <!-- <a href="#/demo/111">111</a> <a href="#/demo/222">222</a>--> <router-link to="/demo/222">2222</router-link> <router-link to="/demo/111">1111</router-link> </div></template><script> export default{ data () { return { user: { username: '', password: '', checkcode: '' }, codeurl: '' } }, methods: { loginUser () { this.$store.commit('setUser',this.user) this.$router.push('/demo/11') /*this.$api.login(this.user).then(function (res) { console.log(res.data) })*/ } } }</script><style scoped></style>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
其中setUser名字必须对应,名字随意,但最好为英文
新手上路,不习勿喷
http://blog.csdn.net/github_26672553/article/details/53265126
- ').addClass('pre-numbering').hide();
阅读全文
0 0
- vuex下state数据的存储
- Vuex state 中数据的实时监控与响应
- vue 没用vuex的情况下解决应用数据方案
- vuex的 module 除了state其他的都合并
- 在Vue-cli里应用Vuex的state和mutations
- React中函数的封装和数据的state数据存储【摄氏度和华氏度转化】
- vue-vuex中使用commit提交mutation来修改state的原因解析
- Vuex(二)--Vue核心概念State
- vuex--state几种定义方式
- 一个“错误”的vuex写法:vuex刷新了数据但是视图没有刷新
- reactjs中state的存储原则
- store vuex 获取数据
- vuex—数据管理器
- 页面刷新的时候vuex里的store对象里的state变量丢失简单处理思路
- 第十课:VueX初始化歌手数据的配置
- Vuex的使用(集中式管理数据)
- 结合localStorage解决vuex页面刷新数据丢失的问题
- android下的数据存储方式
- liunx WordPress 安装(超详细)
- hash_map
- 把一个c类的网络地址192-168-1-0划分为6个子网,请计算出...
- 欢迎使用CSDN-markdown编辑器
- Python time strftime()方法
- vuex下state数据的存储
- 二叉树求序
- time.h 详细介绍
- Lua 开篇
- vim与vi的区别,及常用操作,有查找关键词,向上向下翻页,快速跳到一行首末尾,设置取消行号,撤销编辑
- SVN显示提交时间,提交人等信息
- iFunk:用户全方位享乐体验的升级
- 学习C++要做笔记1(引用)
- 阿里云加密服务使用教程