29

In Vue 3, I created the following Home component, 2 other components (Foo and Bar), and passed it to vue-router as shown below. The Home component is created using Vue's component function, whereas Foo and Bar components are created using plain objects.

The error that I get:

Component is missing template or render function.

Here, the Home component is causing the problem. Can't we pass the result of component() to a route object for 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>

See the problem in codesandbox.

tony19
  • 125,647
  • 18
  • 229
  • 307
Circuit Breaker
  • 3,298
  • 4
  • 17
  • 19

8 Answers8

19

FOR vue-cli vue 3

render function missed in createApp. When setting your app by using createApp function you have to include the render function that include App.

in main.js update to :

FIRST change the second line in javascript from:-

const { createApp } = Vue

to the following lines:

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

SECOND

Change from :-

const app = createApp({})

to:

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


app.mount("#app")
Ismail Omar
  • 437
  • 4
  • 6
11

When app.component(...) is provided a definition object (the 2nd argument), it returns the application instance (in order to allow chaining calls). To get the component definition, omit the definition object and provide only the name:

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

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

demo

tony19
  • 125,647
  • 18
  • 229
  • 307
4

Make sure you have added <router-view></router-view> in your #app container.

Pezhvak
  • 9,633
  • 7
  • 29
  • 39
2

The solution was simple on my side, I created a component that was empty, after filling in the template and a simple text HTML code, it was fixed.

1

I had this issue too. It's a timing issue. I added a v-if to create the component when the page is mounted. That fixed it for me.

<review-info
   v-if="initDone"
   :review-info="reviewInfo"
 />

// script
    onMounted(() => {
      initDone = true
    })
0

The solution for me was to upgrade node module vue-loader to version 16.8.1.

Jorr.it
  • 1,222
  • 1
  • 14
  • 25
0

I was extending a Quasar component in Vue 3, and ran into this problem. I solved it by adding the setup: QInput.setup line last in the component options.

<script>
import { defineComponent } from 'vue'
import { QInput } from 'quasar'

const { props } = QInput

export default defineComponent({
  props: {
    ...props,
    outlined: {
      type: Boolean,
      default: true
    },
    dense: {
      type: Boolean,
      default: true
    },
    uppercase: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    modelValue (v) {
      this.uppercase && this.$emit('update:modelValue', v.toUpperCase())
    }
  },
  setup: QInput.setup
})
</script>
Phil Cairns
  • 758
  • 4
  • 15
0

You need to create separate class (App.vue) for doing the render thing.

Check it below example out:

./views/App.vue

    <template>
  <main>
      <router-view>
      </router-view>
  </main>
</template>

router.js

import { createRouter, createWebHistory } from 'vue-router'

import ExampleComponent from './components/ExampleComponent.vue';


const routes = [
    {
        path: '/',
        name: 'home',
        meta: {
          title: "Home",
      },
        component: ExampleComponent
    }
];

export default createRouter({
  history: createWebHistory(),
  routes
})

app.js (OR main.js)

require('./bootstrap');

import { createApp } from 'vue';
import router from './router'
import App from "./views/App.vue";

createApp(App)
.use(router).mount('#app');

That's it.

nahoang
  • 2,400
  • 1
  • 18
  • 22