【发布时间】:2021-02-01 03:45:09
【问题描述】:
在 Vue 3 中,我创建了以下 Home 组件、2 个其他组件(Foo 和 Bar),并将其传递给 vue-router,如下所示。 Home 组件是使用 Vue 的 component 函数创建的,而 Foo 和 Bar 组件是使用普通对象创建的。
我得到的错误:
Component is missing template or render function.
在这里,Home 组件导致了问题。我们不能将component() 的结果传递给vue-router 的路由对象吗?
<div id="app">
<ul>
<li><router-link to="/">Home</router-link></li>
<li><router-link to="/foo">Foo</router-link></li>
<li><router-link to="/bar">Bar</router-link></li>
</ul>
<home></home>
<router-view></router-view>
</div>
<script>
const { createRouter, createWebHistory, createWebHashHistory } = VueRouter
const { createApp } = Vue
const app = createApp({})
var Home = app.component('home', {
template: '<div>home</div>',
})
const Foo = { template: '<div>foo</div>' }
const Bar = { template: '<div>bar</div>' }
const router = createRouter({
history: createWebHistory(),
routes: [
{ path: '/', component: Home },
{ path: '/foo', component: Foo },
{ path: '/bar', component: Bar },
],
})
app.use(router)
app.mount('#app')
</script>
在codesandbox 中查看问题。
【问题讨论】:
标签: javascript vue.js vue-router vuejs3 vue-router4