【发布时间】:2021-01-25 14:40:15
【问题描述】:
上下文:
我正在构建一个 Vue SPA,我在构建应用程序期间将我的大部分内容设置在一个 json 文件中(可以根据环境变量提供不同的内容)。 我需要为我的 vue 路由器设置该 json 文件中的一些内容!
我遇到的问题是在 json 内容可用之前已经设置了路由。我已经阅读了许多相关的解决方案,但无法得到任何工作......
代码:
完全剥离我的代码版本以了解我当前的设置:
我的主申请文件app.js:
Vue.use(VueRouter);
new Vue({
el: '#app',
store,
router,
methods: {
async storeStaticContent() {
// This is where I fetch the content and write it away in the store.
const response = await fetch(`/static/content.json`);
const json = await response.json();
this.$store.commit(MUTATIONS.SET_CONTENT, json);
},
},
created() {
this.storeStaticContent();
},
});
我的路由器设置router.js:
export const router = new VueRouter({
mode: 'history',
routes: [
{
// The route I need to populate with a string that I write away to the store in my app.js
{
path: `/${store.getters.mainEntity}`,
name: store.getters.mainEntity,
component: EntityPage,
},
{
path: '/*',
name: 'not found',
component: NotFound,
},
}
],
base: '/',
fallback: true,
});
package.json 中的两行表示我正在使用的版本:
"vue": "^2.6.10",
"vue-router": "^3.1.3",
【问题讨论】:
标签: javascript vue.js vuejs2 vuex vue-router