vuejs2:如何将vuex存储传递给vue-router组件

时间:2018-04-21 19:53:26

标签: vuejs2 vue-router vuex

如果未使用vue-router,则在声明new Vue()时可以将商店传递给子组件

但我同时使用vue-routervuex。在这种情况下,如何向store提供components。对于例如我的store.js很典型:

import Vue from 'vue'
import Vuex from 'vuex'
import jwt_decode from 'jwt-decode'
import axios from 'axios'
import VueAxios from 'vue-axios'

Vue.use(Vuex);
Vue.use(VueAxios, axios);

export const store = new Vuex.Store({
  state: {
    jwt: localStorage.getItem('t'),
    endpoints: {
      obtainJWT: 'http://0.0.0.0:8000/auth/obtain_token',
      refreshJWT: 'http://0.0.0.0:8000/auth/refresh_token'
    }
  },
  mutations: {
    updateToken(state, newToken){
      localStorage.setItem('t', newToken);
      state.jwt = newToken;
    },
    removeToken(state){
      localStorage.removeItem('t');
      state.jwt = null;
    }
  },
  actions:{
    obtainToken(username, password){
    //commented code
    },
    refreshToken(){
    //commented code
    },
    inspectToken(){
    //commented code
    }
  }
  });

我的main.js文件如下:

import Vue from 'vue'
import App from './App'
import router from './router'

Vue.config.productionTip = false

import { store } from './store'
console.log(store)
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})

router / index.js文件如下:

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Signup from '@/components/signup/Signup'
import store from '../store.js'

Vue.use(Router)

export default new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      name: 'HelloWorld',
      component: HelloWorld
    },
    {
      path: '/login',
      name: 'Login',
      component: function (resolve) {
        require(['@/components/login/Login.vue'], resolve)
      }
    },
    {
      path: '/signup',
      name: 'Signup',
      component: Signup
    }
  ]
})

现在我如何将store传递给我的Signup组件。即使我在new Vue()传递商店,但在Signup组件

中也无法使用

1 个答案:

答案 0 :(得分:2)

  

我认为问题在于您导入商店并使用../store.js,但是当您导入js文件时,您不必使用.js,因此它必须是import store from '../store' < / p>

您也不必使用vue-router在组件中传递vuex存储。 请按照以下安装vuex store和vue-router进行安装!

Vuex商店:

import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export const store  = new Vuex.Store({
    state: {
        propertiesName: 'PropValue'
    },
    getters: {},
    mutations: {},
    actions: {}
});

的Vue-路由器:

import Vue from 'vue'
import Router from 'vue-router'
import Page from '@/components/Page.vue'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/Page',
      name: 'Page',
      component: Page,
    },
    //other routes
  ],
  mode: 'history',
  scrollBehavior(to, from, savedPosition) {
    if(savedPosition){ //when use press back button will go at the position he was on the page
        return savedPosition
    }
    if(to.hash){ //if has a hash positition to go
        return { selector: to.hash } //go to the page in scrolled Position
    }
    return { x:0, y: 0 } //go to the page in scroll = 0 Position
  }  
})

main.js:

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import { store } from '../store/store'

new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: { App }
})

注意:这样做,现在您可以访问路由器并存储在所有组件中

要在您的组件中使用商店:

this.$store.state.propertiesName

要在组件中使用路由器:

this.$router.push({name: 'Page'})