Laravel 5.4 Vue.JS 无法挂载组件:未定义模板或渲染函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46103337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:37:30  来源:igfitidea点击:

Laravel 5.4 Vue.JS Failed to mount component: template or render function not defined

laravelvue.js

提问by Carlos F

Starting with Vue.js and wanted to give it a try to the example that comes with laravel.

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

No component is displayed and in console I get

没有显示任何组件,在控制台中我得到

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

found in

---> <Example>
   <Root>

Not a fresh install, upgraded from 5.2->5.3->5.4

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

resources/assets/js/app.js

资源/资产/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'
});

resources/assets/js/components/Example.vue

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

This is the blade in which I have the js

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

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

采纳答案by cssBlaster21895

It has to do with the way you are mounting Vue. One time it needs compiler and another time not https://vuejs.org/v2/guide/installation.html#Runtime-Compiler-vs-Runtime-only.

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

You could try my way (it is pure vue project, made from vue webpack template):

你可以试试我的方法(它是纯 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 }
})

This will mount component in global object.

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

回答by Abhishek kushwaha

It should be require('./components/Example.vue').default. Since v13, vue-loaderexports the component as the default key, which still works the same when using import, but requires the above when using require.

应该是require('./components/Example.vue').default。从 v13 开始,vue-loader将组件导出为默认键,使用时仍然相同import,但使用时需要以上内容require

回答by Golam Sorwar

Try this

尝试这个

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

回答by clusterBuddy

As of now, the plaster I have is to add a 'default()' to all Vue object's component.

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

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

Running this package:

运行这个包:

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

Throw in your thumbs-up for using VUEX ;P

为使用 VUEX 点赞;P

回答by Mayank Dudakiya

I have resolved this issue easily by using .defaultmethod at the end of the require().defaultin the following line.

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

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

app.js

应用程序.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', 
});

回答by afshindadashnezhad

Instead of

代替

Vue.component('example', require ('./components/Example.vue'));

Use this

用这个

Vue.component ('example', require ('./components/Example.vue').default);

回答by khalil kasmi

  • delete the folder node_modules
  • run npm install
  • add the following line to package.json

    "webpack": "cross-env NODE_ENV=development webpack --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
    
  • npm run webpack

  • npm run watch

  • dont change anything else

  • 删除文件夹 node_modules
  • 运行 npm 安装
  • 将以下行添加到 package.json

    "webpack": "cross-env NODE_ENV=development webpack --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
    
  • npm 运行 webpack

  • npm 运行监视

  • 不要改变任何其他东西