簡體   English   中英

將數據從父模式傳遞到子模式 - Vue.js

[英]Passing data from Parent to Child modal - Vue.js

我試圖將一些數據從父組件傳遞到作為模態的子組件。 出於某種原因,向我拋出的錯誤是“Uncaught TypeError: Cannot set property 'domain' of undefined”。 我正在嘗試使用其鍵傳遞父組件項,然后在父組件中找到此元素,然后在子組件中找到數據變量。 有人可以告訴我我錯過了什么嗎? 謝謝!

這是我的父組件 Domain.vue

<template>
    <div class="container">
        <div class="form-group">
                <input type="text" class="form-control" id="filter" placeholder="Filter the Domains">  
        </div>

        <div class="row content-holder">
            <table>
                <thead>
                    <tr>
                        <th class="client-name">Domain</th>
                        <th class="client-pm">Client</th>
                        <th class="client-pm">Add Log on Domain</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="domain in domains" :key="domain.id" :domain="domain" class="tr-table">
                        <td class="client-name">{{ domain.url }}</td>
                        <td class="client-pm">{{ domain.client }}</td>
                        <td class="client-pm center"><i class="fas fa-plus jobs-page" data-toggle="modal" @click="openAdd(domain)"></i></td>
                    </tr>
                </tbody>
            </table>
        </div>

        <Add :openmodal="addActive" @closeRequest='close'></Add>

    </div>
</template>

<script>

import axios from 'axios';
let Add = require('./Add.vue');

    export default {

        name:'Domain',

        data(){
            return {

                addActive:'',
                domains: [],
                domain: {id:'', url:'', client: ''},
                errors:{}
            }
        },

        methods:{

            getDomains(){
                window.axios.get('/develogger-app/public/api/domains').then(({data})=>{
                    data.forEach(domain =>{
                        this.domains.push(domain)
                    });
                });
            },

            openAdd(key){
                this.$children[1].domain = this.domains[key];
                this.addActive = 'is-active';

            },

            save(){

            },

            close(){
                this.addActive = '';
            },

        },

        created(){
            this.getDomains();
        },



        components:{
            Add
        }



    }



</script>

這是子組件 Add.vue

<template>
        <!-- Modal -->
            <div :class="openmodal" class="modal fade" id="exampleModalCenter" tabindex="-1" >
                <div class="modal-dialog modal-dialog-centered" role="document">
                    <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="exampleModalLongTitle">Create new Log</h5>
                        <button type="button" class="close">
                            <i class="fas fa-times" @click="close"></i>
                        </button>

                    </div>
                    <div class="modal-body">
                        <form method="post">
                            <input type="hidden" name="_token" :value="csrf">

                            <input name="website" type="text" id="website" class="form-control" placeholder="Log Title"><br>

                            <select id="type" class="form-control" name="type"><br>
                                <option></option>
                            </select>

                            <br>

                            <select id="type" class="form-control" name="type"><br>
                                <option value="" disabled selected>Type</option>
                                <option>Client Update</option>
                                <option>Dev Update</option>
                                <option>Bug</option>
                                <option>Style Fix</option>
                            </select>

                            <br>

                            <label class="left" for="description">Log Description:</label>
                                <textarea class="form-control" rows="5" id="description" name="description"></textarea>
                            <br>

                            <div class="left">
                                <input  type="checkbox" name="tell-everyone" id="tell-everyone">
                                <label for="description">Tell Everyone?</label>
                                <br>
                                <input type="checkbox" name="status" id="status" value="checked">
                                <label for="checked">Resolved and Tested?</label>
                            </div>
                        </form>   
                    </div>
                    <div class="modal-footer">
                        <button  id="log-it" type="button" class="btn btn-circle btn-xl" data-dismiss="modal">
                            <span  id="button-content"><b>LOG IT</b></span>
                            <span  id="button-content"><b>FIX IT</b></span>
                        </button>
                    </div>
                    </div>
                </div>
            </div>
</template>

<script>
export default {
    // checked:false,
    name:'Add',
    props:['openmodal'],

    data(){
            return{
                domain:'',
                csrf: document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
            }
    },

     methods:{
        close(){
            this.$emit('closeRequest');
        }
    },

    // computed: {
    //         isComplete () {
    //             return this.log.title && this.log.domain_id && this.log.type && this.log.description;
    //         }
    //     },
}
</script>

您將data指定為主 Vue 實例中的函數。 它應該是

主 Vue 實例中的一個對象

data: {
    ....
}

而組件中的一個函數

data() {
    return {...}
}

官方參考

您將data作為兩者的功能。

暫無
暫無

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

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