vue-router: Failed to mount component: template or render function not defined. (found in root instance)
I write it in this case, but it is wrong. http://router.vuejs.org/en/essentials/getting-started.html
"vue": "^2.0.1",
"vue-router": "^2.0.0",
"vuex": "^2.0.0"
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id="app">
<h1>Hello App!</h1>
<p>
<!-- use router-link component for navigation. -->
<!-- specify the link by passing the `to` prop. -->
<!-- <router-link> will be rendered as an `<a>` tag by default -->
<router-link to="/foo">Go to Foo</router-link>
<router-link to="/bar">Go to Bar</router-link>
</p>
<!-- route outlet -->
<!-- component matched by the route will render here -->
<router-view></router-view>
</div>
</body>
</html>
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
// 0. If using a module system, call Vue.use(VueRouter)
// 1. Define route components.
// These can be imported from other files
const Foo = { template: '<div>foo</div>' }
const Bar = { template: '<div>bar</div>' }
// 2. Define some routes
// Each route should map to a component. The "component" can
// either be an actual component constructor created via
// Vue.extend(), or just a component options object.
// We'll talk about nested routes later.
const routes = [
{ path: '/foo', component: Foo },
{ path: '/bar', component: Bar }
]
// 3. Create the router instance and pass the `routes` option
// You can pass in additional options here, but let's
// keep it simple for now.
const router = new VueRouter({
routes // short for routes: routes
})
// 4. Create and mount the root instance.
// Make sure to inject the router with the router option to make the
// whole app router-aware.
const app = new Vue({
router
}).$mount('#app')
// Now the app has started!
open url http://localhost:3000/admin/#/bar
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 29
- Comments: 39 (4 by maintainers)
if you use require in router, you need add a
.defaultafter require expression.Hi, thanks for filling this issue. If you
import Vue from 'vue';you get the runtime only build that cannot compile templates (either in-dom templates ortemplateoption). In this case you need the standalone build, here is a helpful section explaining this: https://vuejs.org/guide/installation.html#Standalone-vs-Runtime-only-Build一群中国人在这里用英语交流,也是醉了哈
If you’re using Webpack 2 to build your application, here is what I did to resolve the problem:
From
webpack.config.js:From there, you are able to install Vue as you would expect using ES6 syntax:
import Vue from 'vue';For anyone still finding this thread, this is how I ended up getting past this error for my browserify and typescript setup:
@wspl thanks 正解 the right answers add the require .default routes: [ { path: ‘/’, component: require(‘…/pages/Main.vue’).default } ]
@xqyww123 If you’re using the runtime-only build, anything put inside html cannot be compiled. Therefore, you’ll need the
mountmethodA simple example:
If you need to add a
router/storeinstance to the root instance, it’ll have to be slightly modified:The above is the javascript approach. However, if you want to avoid the rest spread operator / Object.assign, another approach would be using vue’s render functions:
@SageSanyue 你可以尝试使用webpack的动态导入语法:
component: () => import (‘./HelloWorld’)
删除你的module文件夹,重新更新安装所有依赖(使用npm进行安装)。请不要通过cnpm进行安装依赖,这会导致有些包找不到地址(无法正常找到)。推荐使用npm,更换镜像地址即可!
I was getting the same error but adding .default helped as well. Here is my app.js in case it helps anyone else:
i ran into same issue, but my solution was this:
https://stackoverflow.com/questions/49138501/vue-warn-failed-to-mount-component-template-or-render-function-not-defined-i
i think it’s same as in @molerat619 example
Smilar problem, when I write:
let routes = [{ path: ‘/’, component: require(‘./components/member.vue’) }] it failed:[Vue warn]: Failed to mount component: template or render function not defined. add “.default”, it works!
let routes = [{ path: ‘/’, component: require(‘./components/member.vue’).default }] PS:“vue”: “^2.5.2”,“vue-router”: "^3.0.1"“vue-loader”: “^13.3.0”,
I solve mine by adding default after closing curly bracket. ‘.default’. e.g. const routes = [ { path: ‘/foo’, component: Foo }.default, { path: ‘/bar’, component: Bar }.default ]
@skinnn, maybe you must be missing default keyword when you use like that: require(‘./components/Example.vue’).default
as @wspl commented, it would work well.
another option is ‘import’ as following:
It works!!! good!
In my case, I imported my component (in router) as:
It is simply solved if I changed it to
@molerat619 你可以换一种提问题的方式?
期望 -> 现状 -> 结果
—代码—
—问题描述—
你应该尽可能详细的描述你遇到的问题。
I got this error because I had empty
stylesection. Fill thestylesection or remove it and the problem will solved.I still have this issue. This is my
app.js:components/index.js:I get this issue only when I enable esModul in webpack like so:
mix.config.vue.esModule = true;How can I fix this? The routes should be fine, I use import.
@ubershmekel Not to take us too off-topic, but how do you get typings with that setup?
@fnlctrl
Therefore, you'll need the mount method. You mean instance method$mountcan compiled anything inside html? I make a demo and find this wrong. Runtime-only build with method $mount cann’t compiled anything inside html. My code:// index.html
// main.js
// result vue.runtime.common.js:519 [Vue warn]: Failed to mount component: template or render function not defined. Your demo is very nice, thanks.