开发者问题收集

Laravel,Vue 组件未加载

2020-05-24
1808

最初,vue 组件正常运行并加载,但突然我开始收到此错误 [Vue 警告]:未知自定义元素:- 您是否正确注册了组件?对于递归组件,请确保提供“名称”选项。我没有更改任何代码,但在新文件上也收到此错误。

<template>
<v-app>
    <div class="example-component">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">Example Component</div>

                    <div class="card-body">
                        I'm an example component.
                    </div>
                </div>
            </div>
        </div>
    </div>
</v-app>
</template>

<script>
    export default {
        mounted() {
            console.log('Component mounted.')
        }
    }
</script>
@extends('layouts.app')

@section('content')
<div id="app">
<ExampleComponent />
</div>
@endsection
/**
 * 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');

/**
 * The following block of code may be used to automatically register your
 * Vue components. It will recursively scan this directory for the Vue
 * components and automatically register them with their "basename".
 *
 * Eg. ./components/ExampleComponent.vue -> <example-component></example-component>
 */

// const files = require.context('./', true, /\.vue$/i)
// files.keys().map(key => Vue.component(key.split('/').pop().split('.')[0], files(key).default))

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

/**
 * 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.
 */

const app = new Vue({
    el: '#app',
});

这是我的 vue 文件、我想在其中使用 vue 组件的 home.blade.php 文件以及我的 app.js 文件

2个回答

组件的名称是“example-component”,因此您应该将

@section('content')
<div id="app">
<ExampleComponent />
</div>
@endsection

更改为

@section('content')
<div id="app">
<example-component />
</div>
@endsection
Igor Moraru
2020-05-24
  1. 检查您的组件名称是否为 Imported

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

  3. 在此导入组件中,名称为 example-component 有时 您需要运行命令 npm update vue-loader

Muhammad Azam
2021-11-10