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 = api
Vue.config.productionTip = false
Vue.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

Logo

前往低代码交流专区

更多推荐