繁体   English   中英

从子组件更新数据-Vue.js / Axios / Laravel

[英]Update Data from Child component - Vue.js/Axios/Laravel

我正在尝试更新来自Vue.js Laravel应用程序上的子组件的数据,由于某种原因,我无法直接使用它。 检查员的退货告诉我

从空值创建默认对象

父组件打开一个模态,该模态是子组件,然后必须通过update()方法更新信息。 有人可以帮助我了解我所缺少的吗?

基本上,这是我数据库的img,以了解其结构:

在此处输入图片说明

这些方法在我的父组件Log.vue中,这就是我将数据从父组件传递给子组件的方式:

<log-edit v-if="editModalOpen" :logId="logId" :logUser="logUser" :logTitle="logTitle" :logType="logType" :logDescription="logDescription" :logDate="logDate" @closeRequest='close'></log-edit>

<td @click="openEdit(log.id,log.user,log.title,log.type,log.description,log.created_at)"><i class="fas fa-edit"></i></td>
<script>


methods:{
 openEdit(id,user,title,type,description,date){
                    this.logId = id;
                    this.logUser = user;
                    this.logTitle = title;
                    this.logType = type;
                    this.logDescription = description;
                    this.logDate = date;
                    this.editModalOpen = true;
                },
}

<script>

这是EditLog.vue,它是从上面的父级接收数据的子级组件:

<template>
    <div class="container">
        <div id="overlay">
            <div class="edit-detail-window">
                <div class="modal-header">
                        <h3 class="modal-title" id="exampleModalLongTitle">{{logTitle}}</h3>
                        <button type="button" class="close">
                        <i class="fas fa-times" @click="close"></i>
                        </button>
                </div>
                <div id="show-detail-modal-body" class="modal-body">
                    <br>
                    <label>Title:</label>
                    <input class="form-control" type="text" v-model="logTitle">

                    <br>
                    <label>Type:</label>
                    <select v-model="logType" class="form-control" ><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>Description:</label>
                    <textarea class="form-control" cols="30" rows="5" v-model="logDescription"></textarea>

                </div>
                <div class="modal-footer">
                    <button d="log-it" type="button" class="btn btn-circle btn-xl" @click="update(logTitle, logType, logDescription)">
                        <span><b>EDIT</b></span>
                    </button>
                </div>
            </div>
        </div>
    </div>
</template>


<script>

import axios from 'axios';

export default {

    name:'EditLog',

    props:['logId','logUser','logTitle','logType','logDescription','logDate'],

    data(){
        return{
            log:{title:'',type:'',description:''},
            errors:{}
        }
    },

    methods:{

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

        update(title,type,description){

            this.log.title = title;
            this.log.type = type;
            this.log.description - description;

            window.axios.patch(`/develogger-app/public/api/logs/${this.logId}`,this.$data.log).then((response)=> this.close())
                    .catch((error) => this.errors = error.response.data.errors)

        }

    }
}
</script>

这是日志路由/api.php

Route::patch('/logs/{id}','LogController@update');

这是LogController.php上的更新功能

public function update($id, Request $request){

        $log = Log::find($request->id);
        $log->title = $request->logTitle;
        $log->type = $request->logType;
        $log->description = $request->logDescription;

        $log->save();

    }

关于如何使它起作用的任何线索?

我在这里注意到的几点,可能太大了,无法发表评论。

首先,不是将log所有单独属性传递给<edit-log>组件,而是将整个对象传递进来可能更容易?

<edit-log :log="log"></edit-log>

其次,您似乎没有将要发送到<edit-log>的prop数据绑定到该组件上的data 我认为您无法直接对道具进行v模型化。

第三,我认为您要在<edit-log>组件中进行更新的位置,只需像this.log而不是this.$data.log那样传递数据。

因此,您的<edit-log>可能看起来像这样

<template>
    <div class="container">
        <div id="overlay">
            <div class="edit-detail-window">
                <div class="modal-header">
                        <h3 class="modal-title" id="exampleModalLongTitle">{{logTitle}}</h3>
                        <button type="button" class="close">
                        <i class="fas fa-times" @click="close"></i>
                        </button>
                </div>
                <div id="show-detail-modal-body" class="modal-body">
                    <br>
                    <label>Title:</label>
                    <input class="form-control" type="text" v-model="log.title">

                    <br>
                    <label>Type:</label>
                    <select v-model="log.type" class="form-control" ><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>Description:</label>
                    <textarea class="form-control" cols="30" rows="5" v-model="log.description"></textarea>

                </div>
                <div class="modal-footer">
                    <button d="log-it" type="button" class="btn btn-circle btn-xl" @click="update()">
                        <span><b>EDIT</b></span>
                    </button>
                </div>
            </div>
        </div>
    </div>
</template>


<script>

import axios from 'axios';

export default {

    name:'EditLog',

    props:['initiaLog'],

    data(){
        return{
            log:this.initialLog,
            errors:{}
        }
    },

    methods:{

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

        update(){

            window.axios.patch(`/develogger-app/public/api/logs/${this.logId}`,this.log)
                .then((response)=> this.close())
                .catch((error) => this.errors = error.response.data.errors)

        }

    }
}
</script>

你会这样调用它

<log-edit v-if="editModalOpen" :initial-log="log" @closeRequest='close'></log-edit>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM