我花了一整天試圖找出是什麼使這個例子失敗:Vue的路由器工作erraticaly
Vue.use(VueRouter);
var PlayerDetail = {
template: '<div>FOO</div>'
};
var PlayerList = {
template: '<div>BAR</div>'
};
var routes = [{
path: '/player',
component: PlayerList
}, {
path: '/detail',
component: PlayerDetail
}];
var appRouter = new VueRouter({
routes
});
var appVm = new Vue({
appRouter
}).$mount('#app');
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<div id="app">
<h1>Hello App!</h1>
<p>
<router-link to="/player">foo</router-link>
<router-link to="/detail">bar</router-link>
</p>
<router-view></router-view>
</div>
如果它正是這樣一個工程:
Vue.use(VueRouter);
var Foo = {
template: '<div>FOO</div>'
};
var Bar = {
template: '<div>BAR</div>'
};
var routes = [{
path: '/foo',
component: Foo
}, {
path: '/bar',
component: Bar
}];
var router = new VueRouter({
routes
});
var appVm = new Vue({
router
}).$mount('#app');
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<div id="app">
<h1>Hello App!</h1>
<p>
<router-link to="/foo">foo</router-link>
<router-link to="/bar">bar</router-link>
</p>
<router-view></router-view>
</div>
正如你所看到的,這兩個例子實際上都是相同的代碼,但在第一個例子中,我得到了TypeError:當路由器正在渲染時,route是未定義的。
其他人看到我在這裏失蹤的東西嗎?
非常感謝你 – panietoar