Laravel 5.4 Vue.JS无法安装组件:未定义模板或渲染功能

Car*_*s F 15 laravel vue.js

从Vue.js开始,想尝试一下laravel附带的例子.

没有组件显示在我得到的控制台中

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

found in

---> <Example>
   <Root>
Run Code Online (Sandbox Code Playgroud)

不是全新安装,从5.2-> 5.3-> 5.4升级

资源/资产/ JS/app.js

/**
 * First we will load all of this project's JavaScript dependencies which
 * includes Vue and other libraries. It is a great starting point when
 * building robust, powerful web applications using Vue and Laravel.
 */

require('./bootstrap');

window.Vue = require('vue');

/**
 * Next, we will create a fresh Vue application instance and attach it to
 * the page. Then, you may begin adding components to this application
 * or customize the JavaScript scaffolding to fit your unique needs.
 */
Vue.component('example', require('./components/Example.vue'));

const app = new Vue({
    el: '#app'
});
Run Code Online (Sandbox Code Playgroud)

资源/资产/ JS /组件/ Example.vue

<template>
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">Example Component</div>

                <div class="panel-body">
                    I'm an example component!
                </div>
            </div>
        </div>
    </div>
</div>
</template>

<script>
    export default {
        mounted() {
            console.log('Component mounted.')
        }
    }
</script>
Run Code Online (Sandbox Code Playgroud)

这是我拥有js的刀片

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Testing</title>
    <link rel="stylesheet" href="css/app.css">
    <meta name="csrf-token" content="{{ csrf_token() }}">
</head>
    <body>
    <div id="app">
        <example></example>
    </div>
        <script src="js/app.js" charset="utf-8"></script>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

webpack.mix.js

let mix = require('laravel-mix');

mix.js('resources/assets/js/app.js', 'public/js')
    .sass('resources/assets/sass/app.scss', 'public/css');
Run Code Online (Sandbox Code Playgroud)

小智 41

它应该是require('./components/Example.vue').default.从v13开始,vue-loader将组件导出为默认密钥,在使用时仍然可以正常工作import,但在使用时需要使用上述密钥require.


小智 11

尝试这个

Vue.component('example-component', require('./components/ExampleComponent.vue').default);
Run Code Online (Sandbox Code Playgroud)

  • 在组件末尾添加`.default`可以解决laravel应用中的问题。谢谢 (3认同)

clu*_*ddy 10

到目前为止,我所拥有的灰泥是为所有Vue对象的组件添加一个“ default()”。

Vue.component('form-component', require('./components/FormComponent').default);
Vue.component('gratitude-pop', require('./components/GratitudePop').default);//..etc..etc
Run Code Online (Sandbox Code Playgroud)

运行此程序包:

  "devDependencies": {
        "axios": "^0.18",
        "bootstrap": "^4.0.0",
        "cross-env": "^5.1",
        "jquery": "^3.2",
        "laravel-mix": "^4.0.7",
        "lodash": "^4.17.5",
        "popper.js": "^1.12",
        "resolve-url-loader": "^2.3.1",
        "sass": "^1.15.2",
        "sass-loader": "^7.1.0",
        "vue": "^2.5.17"
    },
    "dependencies": {
        "vuex": "^3.0.1"
    }
Run Code Online (Sandbox Code Playgroud)

赞成使用VUEX; P

  • 我不知道为什么这样做,但确实如此。谢谢!我只需要向单个组件(即我的父母)添加“默认”即可。另外,仅当我更新了webpack中的依赖项时才出现此错误,尤其是刚才的“ laravel-mix”:“ ^ 4.0.13”。一些新要求? (4认同)

css*_*895 6

它与你安装Vue的方式有关.有一次它需要编译器而另一次不需要https://vuejs.org/v2/guide/installation.html#Runtime-Compiler-vs-Runtime-only.

您可以尝试我的方式(这是纯vue项目,由vue webpack模板制作):

import Vue from 'vue'
import App from './App'
import Example from './components/Example'
import router from './router' //this will import router/index.js

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  template: '<App/>',
  components: { App, Example }
})
Run Code Online (Sandbox Code Playgroud)

这将在全局对象中安装组件.


May*_*iya 6

通过在下一行的末尾使用.default方法, 我已经轻松解决了此问题 require().default

Vue.component('example-component', require('./components/ExampleComponent.vue').default);
Vue.component('articles', require('./components/ArticleComponent.vue').default);
Run Code Online (Sandbox Code Playgroud)

app.js

/**
 * First we will load all of this project's JavaScript dependencies which
 * includes Vue and other libraries. It is a great starting point when
 * building robust, powerful web applications using Vue and Laravel.
 */

require('./bootstrap');

window.Vue = require('vue');

/**
 * Next, we will create a fresh Vue application instance and attach it to
 * the page. Then, you may begin adding components to this application
 * or customize the JavaScript scaffolding to fit your unique needs.
 */

Vue.component('example-component', require('./components/ExampleComponent.vue'));
Vue.component('articles', require('./components/ArticleComponent.vue').default);

const app = new Vue({
    el: '#app', 
});
Run Code Online (Sandbox Code Playgroud)


小智 6

代替

Vue.component('example', require ('./components/Example.vue'));
Run Code Online (Sandbox Code Playgroud)

用这个

Vue.component ('example', require ('./components/Example.vue').default);
Run Code Online (Sandbox Code Playgroud)