解决vue不相关组件传递数据并保存(state),解决报错this.$store.commit is not a function
来源:互联网 发布:javbus最新域名2017 编辑:程序博客网 时间:2024/05/29 19:01
1。在src文件下创建一个store文件,里面加入一个store.js文件
。内容大致为 ., mutations里面吧所需要的程序处理好,外部直接传值即可
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; } }})
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 }})
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>
其中setUser名字必须对应,名字随意,但最好为英文
新手上路,不习勿喷
http://blog.csdn.net/github_26672553/article/details/53265126
http://www.cnblogs.com/jasonwang2y60/p/6433082.html
阅读全文
1 0
- 解决vue不相关组件传递数据并保存(state),解决报错this.$store.commit is not a function
- 如何解决Uncaught TypeError: this.setState is not a function
- 解决vue不相关组件之前数据传输问题
- Extjs 报错 this.addEvents is not a function
- 微信小程序 报错:this.setData is not a function
- react:undefined is not a function(this.State({flag:true,}))
- Vue+iview报TypeError: this.$parent.updateGutter is not a function
- JS报错 Uncaught TypeError: undefined is not a function,解决
- React Native 报错 singleValue.stopTracking is not a function -解决
- 解决Zepto.js不支持animate报错的问题,错误信息$(...).animate is not a function
- $ is not a function 已解决
- RVM is not a function 解决
- 解决object is not a function
- 解决 subject.onNext is not a function
- undefined is not a function错误解决
- Object.assign is not a function(解决)
- Extjs 报this.addEvents is not a function错误原因
- React中报this.isMounted is not a function
- 从打游戏的显卡,到科学先锋,一篇文章读懂异构计算
- JAVA反射的实现方式
- 嵌入式操作系统
- 初学者如何从零学习人工智能?
- 深入Java关键字null
- 解决vue不相关组件传递数据并保存(state),解决报错this.$store.commit is not a function
- Html
- webservice
- 进程的虚拟内存,物理内存,共享内存
- Fried Fish
- Spring Aop实例之xml配置
- 搜索式抓取豆瓣影片播放来源
- Android_横向滑动菜单, HorizontalScrollView,Fragment,Viewpager结合使用
- JS类选择