Vue.js - 组件缺少模板或渲染函数

Vue.js - Component is missing template or render function

在 Vue 3 中,我创建了以下 Home 组件和 2 个其他组件(FooBar),并将其传递给 vue-router,如下所示。 Home 组件是使用 Vue 的 component 函数创建的,而 FooBar 组件是使用普通对象创建的。

我得到的错误:

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中的问题。

app.component(...) 提供定义对象(第二个参数)时,它 returns 应用程序实例(以便允许链接调用)。要获取组件定义,请省略定义对象并仅提供名称:

app.component('home', { /* definition */ })
const Home = app.component('home')

const router = createRouter({
  routes: [
    { path: '/', component: Home },
    //...
  ]
})

demo

适用于 vue-cli vue 3

createApp 中缺少渲染函数。 使用 createApp 函数设置您的应用程序时,您必须包含包含 App.

的渲染函数

在main.js 更新为:

第一个 将 javascript 中的第二行从:-

更改为
const { createApp } = Vue

到以下几行:

import { createApp,h } from 'vue'
import App from './App.vue'

第二个

更改自:-

const app = createApp({})

至:

const app  = createApp({
    render: ()=>h(App)
});


app.mount("#app")

我这边的解决方案很简单,我创建了一个空的组件,在填写模板和简单的文本 HTML 代码后,它就修复了。

确保您已将 <router-view></router-view> 添加到 #app 容器中。

我的解决方案是将节点模块 vue-loader 升级到版本 16.8.1。