[英]Gulp [Vue warn]: Failed to mount component: template or render function not defined
I have read over several SO questions about this with no luck.我已经阅读了几个关于此的 SO 问题,但没有运气。
I'm trying to run Vue with Gulp and no webpack but I keep getting the error.我正在尝试使用 Gulp 而没有 webpack 运行 Vue,但我不断收到错误消息。 [Vue warn]: Failed to mount component: template or render function not defined.
and [Vue warn]: Cannot find element: #app
和[Vue warn]: Cannot find element: #app
My structure is:我的结构是:
project
|-- src
|-- app
|-- components
-- app.vue
|-- js
-- main.js
index.pug
package.json
{
"name": "boilerplate",
"version": "0.0.1",
"dependencies": {
"babel": "^5.1.5",
"babel-runtime": "^5.1.9",
"vue": "^2.1.8"
},
"devDependencies": {
"babel-core": "^6.21.0",
"babel-preset-es2015": "^6.18.0",
"babelify": "^7.3.0",
"browserify": "^13.3.0",
"css-loader": "^0.26.1",
"gulp": "^3.9.1",
"gulp-clean": "^0.3.1",
"gulp-concat": "^2.5.2",
"gulp-connect": "^5.0.0",
"gulp-load-plugins": "^1.3.0",
"gulp-pug": "^3.2.0",
"gulp-require-tasks": "^1.0.5",
"gulp-util": "^3.0.4",
"run-sequence": "^1.2.2",
"vinyl-source-stream": "^1.1.0",
"vue-loader": "^10.0.2",
"vue-template-compiler": "^2.1.9",
"vueify": "^9.2.4"
},
"browserify": {
"transform": [
"vueify",
"babelify"
],
"browser": {
"vue": "vue/dist/vue.common"
}
}
}
javascript.js
(which is a task in my gulpfile) javascript.js
(这是我 gulpfile 中的一项任务)
var plugin = require('gulp-load-plugins')({
camelize: true
}),
browserify = require('browserify'),
babelify = require('babelify'),
vueify = require('vueify'),
source = require('vinyl-source-stream');
paths = {
js: 'src/app/js/main.js'
};
module.exports = function( gulp, cb ) {
return browserify({
debug: true,
entries: ['./src/app/js/main.js'],
})
.transform(babelify)
.transform(vueify, {
template: 'pug'
})
.bundle()
.pipe(source('main.js'))
.pipe(gulp.dest('./build/js'))
.pipe(plugin.connect.reload())
.on('error', function( error ) {
console.error(String( error ));
});
};
main.js
import Vue from 'vue';
import App from './../components/app.vue'
new Vue({
name: 'app',
el: '#app'
}).$mount('#app');
index.pug
doctype html
html
head
meta(charset='utf-8')
meta(http-equiv='X-UA-Compatible', content='IE=edge')
title Lexicon Riot
meta(name='viewport', content='width=device-width')
body
#app
script(src='js/main.js')
app.vue
template
div
h1 {{ msg }}
Does anyone have any suggestion as to what could be going wrong?有人对可能出现的问题有任何建议吗?
So I had the same problem when using webpack.所以我在使用 webpack 时遇到了同样的问题。 The issue comes from the runtime version of Vue not providing that method, instead you need to use the standalone version.问题来自 Vue 的运行时版本没有提供该方法,而是您需要使用独立版本。 Then compile them before going to production.然后在投入生产之前编译它们。
I fixed this with adding我通过添加解决了这个问题
resolve: {
alias: {
'vue$': 'vue/dist/vue.common.js'
}
}
to my webpack.config.js到我的 webpack.config.js
Info found here: https://v2.vuejs.org/v2/guide/installation.html#Standalone-vs-Runtime-only-Build信息在这里找到: https ://v2.vuejs.org/v2/guide/installation.html#Standalone-vs-Runtime-only-Build
This should help you这应该可以帮助你
new Vue({
render: (h) => h(App)
}).$mount('#app')
So remove this part from main.js
:所以从main.js
中删除这部分:
new Vue({
name: 'app',
el: '#app'
}).$mount('#app');
And put one I provided above.并将我提供的一个放在上面。
So my issue was that in the .vue
file I was using所以我的问题是在我使用的.vue
文件中
template
instead of代替
<template>
So that in combination with the webpack.config.js
solves my issue所以结合webpack.config.js
解决了我的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.