为什么Vue应用程序未在刷新时挂载?

时间:2017-03-22 21:57:46

标签: javascript vue.js vuejs2 vue-router

我的Vuejs SPA应用程序包含以下代码:

...
import Rides from './components/rides'
import RideInfo from './components/ride_info'
...

//Defining router
const router = new VueRouter({
    mode: 'history',
    routes: [
        { path: '/rides/:car', component: Rides, name: 'rides' },
        { path: '/ride_info/:ride_index', component: RideInfo, name: 'ride_info' }
    ]
});

//Defining vuex store
const store = new Vuex.Store({
    state: {
        isLoggedIn: !!localStorage.getItem("token"),
        data: {}
    },
    mutations: {
        login (state) {
            state.isLoggedIn = true;
        },
        logout (state) {
            state.isLoggedIn = false;
        },
        set_initial_data(state, data) {
            state.data = data;
        }
    },
    actions: {
        login({ commit }, token) {
            localStorage.setItem("token", token);
            commit("login");
        },
        logout({ commit }) {
            localStorage.removeItem("token");
            commit("logout");
        },
        set_initial_data({commit}, data) {
            commit("set_initial_data", data);
        }
    },
    getters: {
        isLoggedIn: state => {
            return state.isLoggedIn
        },
        data: state => {
            return state.data
        }
    }
});

//Starting the app
const app = new Vue({
    router,
    store,
    mounted: function() {
        console.log("app mounted");
        var that = this;
        $.get("/initial_data/", {}, function(data) {
            that.$store.dispatch('set_initial_data', data);
        });
    }
}).$mount('#app');

当我转到http://localhost:8000/rides/123并刷新时,我进入已安装的功能并获取我应该的初始数据。

当我转到http://localhost:8000/ride_info/123并刷新时,我没有进入已安装的功能而没有带来数据。

知道为什么会这样,我怎样才能让应用程序在每次刷新时都进入挂载函数而不管url?

0 个答案:

没有答案