【发布时间】:2017-05-25 03:09:11
【问题描述】:
我无法让我的子视图在 Vue 中呈现。
我的 main.js 文件看起来像这样
import DashboardProducts from './components/Dashboard/DashboardProducts'
import DashboardSettings from './components/Dashboard/DashboardSettings'
Vue.use(VueRouter)
Vue.use(Vuex)
const routes = [
{ path: '/activate', component: Activate },
{ path: '/dashboard/:view', component: Dashboard,
children: [
{ path: 'products', component: DashboardProducts },
{ path: 'settings', component: DashboardSettings }
]
},
{ path: '/login', component: Login },
{ path: '/account', component: UserAccount }
];
const router = new VueRouter({
routes // short for routes: routes
});
export default router;
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
store,
render: h => h(App)
});
如您所见,我已经导入了组件并且没有出现错误。我还将它们添加为 Dashboard 的子项并设置它们的路径。
在我的 Dashboard.vue 视图中我这样做
<template>
<div>
<dashboard-nav></dashboard-nav>
<!-- Will display product and settings components -->
<router-view></router-view>
</div>
</template>
<script>
import DashboardNav from '../components/Dashboard/DashboardNav'
export default {
name: 'Dashboard',
components: {
DashboardNav
}
};
</script>
<style>
</style>
网址匹配,但没有组件正在呈现。我错过了什么?
这是一个 JSFiddle 几乎是我想要的 https://jsfiddle.net/dtac5m11/
那里似乎工作正常,但我也在我的应用程序中使用单文件组件,所以它可能有点不同?
同样,问题是让子组件在其路由匹配时进行渲染。目前没有安装任何组件。
更新:
我正在渲染 DashboardProducts 组件,但无法渲染 DashboardSettings。
谢谢!
【问题讨论】:
-
您能告诉我们是什么问题吗?尝试使用您的内容创建 webpack bin
-
问题是子组件在它们的路由匹配时没有呈现。我在这里有一个 JS Fiddle,它似乎工作正常,但无法弄清楚为什么它在我的应用程序中不起作用jsfiddle.net/dtac5m11
-
我们可能需要查看完整的存储库才能弄清楚。
-
只需使用
/dashboard而不是/dashboard/:view,那么它将适用于/dashboard/settings -
另外,去掉
/settings的斜线,只是path: 'settings'
标签: vue.js vue-router