簡體   English   中英

Laravel - 如何將 Vue 組件導入另一個組件

[英]Laravel - How to Import Vue Component to Another Component

我是Vue.js的新手。 我想了解如何使用component 我試圖將我的組件導入另一個組件,但失敗了。 我正在使用Laravel 5.8 以下是我收到的錯誤。

未找到模塊:錯誤:無法解析 './components/modalAlert.vue

以下是我的代碼。

應用程序.js

Vue.component('form-to-do', require('./components/formToDo.vue').default);
Vue.component('modal-alert', require('./components/modalAlert.vue').default);

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

formToDo.Vue

<template>
// form 

<modal-alert></modal-alert>
</template>

<script>
import modalAlert from './components/modalAlert.vue';

export default {
    components: {
       'modal-alert': modalAlert 
    },

    data() {
        return {
            setErrors: [],
            tasks: [],
            task: {
                id: '',
                name: '',
                completed: '',
                date_completed: ''
            },
            result: '',
            input: {
                name: ''
            }
        };
    },
}
</script>

modalAlert.vue

<template>
    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            Test  
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div>
      </div>
    </div>
</template>

<script>
    export default {
        props: ['id'],
        data() {
            return {

            }
        },

        mounted() {
            console.log('Component mounted.')
        }
    }
</script>

您的組件可能在同一個文件夾中。 在你的組件 formToDo.vue: 改變這個:

import modalAlert from './components/modalAlert.vue';

對此:

import modalAlert from './modalAlert.vue';

為了解決另一個問題,正如@ambianBeing 所建議的那樣,您的組件 formToDo.vue 必須具有根元素才能在其中添加子組件。

<template>
<div> <!-- this is the root -->
    <modal-alert></modal-alert>
</div>
</template>

暫無
暫無

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

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