首页 文章

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

提问于
浏览
8

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

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

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

found in

---> <Example>
   <Root>

不是全新安装,从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'
});

资源/资产/ 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>

这是我拥有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>

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');

5 回答

  • 5

    它与你安装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 }
    })
    

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

  • 24

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

  • 3

    截至目前,我的石膏是为所有Vue对象的组件添加'default()' .

    Vue.component('form-component', require('./components/FormComponent').default);
    Vue.component('gratitude-pop', require('./components/GratitudePop').default);//..etc..etc
    

    运行此包:

    "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"
        }
    

    投入大拇指使用VUEX; P

  • 6

    试试这个

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

    我已经通过在 require().default 末尾使用 .default 方法轻松解决了这个问题 .

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

    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', 
    });
    

相关问题