javascript - Laravel 5.3.10 + Vue.js 2.0.1 上出现 "Failed to mount component” 错误

标签 javascript laravel-5 vue.js

我一直致力于 Laravel + Vue.js。 我想在 Laravel Blade 模板上开发 Vue 组件, 但是当我尝试这样做时,出现了以下错误,并且效果不佳。
* 为了检查的目的,我只编写了最少量的 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')

关于javascript - Laravel 5.3.10 + Vue.js 2.0.1 上出现 "Failed to mount component” 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39910025/

相关文章:

javascript - 在数据表日期列中显示月份名称和年份

php - Laravel 队列独立 "isDownForMaintenance"错误

laravel-5 - Laravel 5.3 - Omnipay Paypal Express 不返回成功消息

javascript - 从页面隐藏 vue 路由器

javascript - 在子 vue 组件数据函数中访问 props?

javascript - 如何在不丢失样式的情况下打印vue组件的一部分

javascript - Tinymce 的替代品

javascript - AngularJS:将服务注入(inject)指令?

javascript - TypeORM:无法读取未定义的属性 'getParameters'

php - 从查询创建多数组