在Laravel 5.3.10 + Vue.js 2.0.1上出现“Failed to mount component”

"Failed to mount component” error on Laravel 5.3.10 + Vue.js 2.0.1

本文关键字:Failed to component mount Laravel js Vue      更新时间:2023-09-26

我一直在开发Laravel + Vue.js。我想开发Vue组件到Laravel刀片模板,但是当我尝试这样做时,发生了以下错误,并且它不能很好地工作。
*为了检查,我只写了一个最小的Vue组件代码。

参考以下文章,我尝试了各种方法。
https://github.com/vuejs/vue-loader/issues/287

正在发生的问题和错误信息

[Vue warn]: Failed to mount component: template or render function not defined. (found in anonymous component - use the "name" option for better debugging messages.)

index.blade.php

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
    <div id="app"></div>
<script src="js/bundle.js"></script>
</body>
</html>

app.js

import Vue from 'vue'
import InputText from './components/InputText.vue'
new Vue({
    render(h) {
      return h(InputText)
    }
}).$mount('#app')

InputText.vue

<template>
    <input class="input-text" type="text">
</template>
<script>
export default {
}
</script>
<style lang="sass" scoped>
</style>

gulp.file.js

const elixir = require('laravel-elixir');
require('laravel-elixir-vue-2');
elixir(mix => {
    mix.sass('**/*.scss')
       .webpack('')
});

webpack.config.js

const webpack = require('webpack');
module.exports = {
  entry: {
    bundle: './resources/assets/js/app.js',
  },
  output: {
    filename: '[name].js',
  },
  resolve: {
    root: __dirname,
    extensions: ['', '.js', '.vue'],
    modulesDirectories: ['node_modules'],
    alias: {
      vue: 'vue/dist/vue.js',
    },
  },
  module: {
    loaders: [
      {
        test: /'.vue?$/,
        loader: 'vue-loader',
      },
      {
        test: /'.js$/,
        loader: 'babel',
        exclude: /node_modules/,
      },
      {
        test: /'.html$/,
        loader: 'vue-html',
      },
      {
        test: /'.(jpg|png)$/,
        loader: 'url-loader?limit=10000',
      },
    ],
  },
  vue: {
    loaders: {
      scss: 'style!css!sass',
    },
  },
};

package.json

"devDependencies": {
    "babel-core": "^6.17.0",
    "babel-loader": "^6.2.5",
    "babel-plugin-transform-runtime": "^6.15.0",
    "babel-preset-es2015": "^6.16.0",
    "babel-runtime": "^6.11.6",
    "laravel-elixir": "^6.0.0-11",
    "laravel-elixir-browsersync-official": "^1.0.0",
    "laravel-elixir-vue-2": "^0.2.0",
    "laravel-elixir-webpack-official": "^1.0.6",
    "vue": "^2.0.1",
    "vue-loader": "^9.5.1",
    "webpack": "^1.13.2"
    ...
}

互补信息

Laravel 5.3.10
node . js 6.4.0
NPM 3.9.3

您需要注册InputText组件,以便在根组件中使用它。这里使用的是ES2015的简写。

import Vue from 'vue'
import InputText from './components/InputText.vue'
new Vue({
    components: {
        InputText
    },
    render(h) {
      return h(InputText)
    }
}).$mount('#app')

在您的webpack.config.jsvue下的loaders旁边添加esModule: true,您应该很好。

或者,您可以使用const Vue = require('vue')代替import Vue from 'vue',它试图从组件生成的模块中加载default导出,并且它无法这样做,除非您告诉vue-loader生成兼容的代码。

实际上这是规范之间更普遍的不兼容,其中export default foo并不意味着与module.exports = foo相同。当您将前者require时,它将给您一个对象{ default: foo },而后者将返回未封装的foo