0

When using vue-router the standard set up is

main.js requires the routes.js file, which will look something like this

//routes.js
import Register from './components/Register'
import Login from './components/Login'

module.exports = [{
            path: `/`,
            component: Login,
        }, {
            path: `/register`,
            component: Register,
        }]

My question is why can I just do

//routes.js
module.exports = [{
            path: `/`,
            component: require('./components/Login'),
        }, {
            path: `/register`,
            component: require('./components/Register'),
        }]

When I try it, I get this console error

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

found in

---> <Anonymous>
       <App> at src/App.vue
         <Root>
Seth McClaine
  • 6,298
  • 4
  • 32
  • 53

1 Answers1

-1

**import and require work differently see the link bellow there are good documentation about the topic. ** es6 features and require how Actually Works **see the document how node js work under the hood how module.export work what is the nodejs design pattern (how node js wrap our code within Immediately invoked function expression and pass code into v8 engine)

you can simply midify your routes.js file

component:() => import('./components/Register.vue') or component: require('./components/Login.vue').default

both will work

[{ path: '/', component:() => import('./components/Register.vue') }, { path: '/register', component: require('./components/Login.vue').default }];