A
A
Alexander Ivanov2018-03-15 11:15:45
Vue.js
Alexander Ivanov, 2018-03-15 11:15:45

Why doesn't webpack find routes by const in vue-cli assembly, but only by import?

Switching from simple to webpack vue assembly
noticed a strange feature in routing

import index from '@/pages/HelloWorld' // работает
const index = require('../pages/HelloWorld.vue') // не работает

const router = new Router({
  mode: 'history',
  scrollBehavior,
  routes: [
    { name: 'index', path: '/', component: index, meta: { scrollToTop: true } }
  ]
});

[Vue warn]: Failed to mount component: template or render function not defined.

found in

---> <Anonymous>
       <Main> at src/main/main.vue
         <Root>

There are suggestions why it does not find by const?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Yarkov, 2018-03-15
@cimonlebedev

First: why HelloWorld.vue extension at the end.
Second: try

const index = require('../pages/HelloWorld.vue')

const router = new Router({
  mode: 'history',
  scrollBehavior,
  routes: [
    { name: 'index', path: '/', component: index.default, meta: { scrollToTop: true } }
  ]
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question