我试图更新来自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();
}
关于如何使它工作的任何线索?
答案 0 :(得分:1)
我在这里注意到的一些观点可能太大了,无法发表评论。
首先,与其将log
的所有单独属性传递给<edit-log>
组件,不如将整个对象传递进来可能更容易?
<edit-log :log="log"></edit-log>
第二,看起来好像没有将要发送到<edit-log>
的道具数据绑定到该组件上的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>