簡體   English   中英

Laravel 5.4 Vue.JS 無法掛載組件:未定義模板或渲染函數

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

從 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/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>

這是我有 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');

它應該是require('./components/Example.vue').default 從 v13 開始, vue-loader將組件導出為默認鍵,在使用import時仍然有效,但在使用require時需要上述內容。

嘗試這個

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

到目前為止,我所擁有的石膏是為所有 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

這與您安裝 Vue 的方式有關。 一次需要編譯器,另一次不需要https://v2.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 }
})

這將在全局對象中安裝組件。

我通過在下一行的require().default末尾使用.default方法輕松解決了這個問題。

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

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

代替

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

用這個

Vue.component ('example', require ('./components/Example.vue').default);
  • 刪除文件夾 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 運行觀察

  • 不要改變任何其他東西

只是在上述答案之上添加一小部分信息。

如果在引發錯誤的組件上添加了.default ,則該錯誤已修復。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM