【发布时间】:2020-01-21 02:42:19
【问题描述】:
我需要在 axios 成功时附加 m 返回的数据,但它只附加了我的代码的一部分,
截图
当我单击保存时,它应该使用不同的按钮再次打印输入字段中保存的数据以进行更新。但它只打印标签
代码
HTML
<template>
<div>
<div class="variationData"></div>
</div>
</template>
script
export default {
data() {
return {
variationParents: [
{
name: '',
type: ''
}
],
variationChilds: [
{
name: ''
}
],
}
},
methods: {
addVariants(e) {
e.preventDefault();
axios.post('/api/admin/variations/store', {
childs: this.variationChilds,
parent: this.variationParents,
})
.then(res => {
console.log(res);
this.isLoading = false;
// print back saved data
$(res.data.data).each(function(_, i){
$(i).each(function(__, ii){
var row = `<el-col :span="24" style="margin-top:15px;">
<el-form-item label="Variation Name">
<el-col :span="12" style="margin-top:15px;">
<el-input placeholder="Please input your variation name" value="${i.name}" class="input-with-select"></el-input>
</div>
</el-col>
<el-col class="line text-center" :span="3">Variation Value(s)</el-col>
<el-col :span="9" style="margin-top:15px;">
<el-input value="${ii.name}" placeholder="Please input your variation value" class="input-with-select"></el-input>
</el-col>
<el-col :span="24" style="margin-top:15px;">
<el-button type="primary" native-type="submit">Update Variations</el-button>
</el-col>
</el-form-item>
</el-col>`;
$('.variationData').html(row);
});
});
//
})
.catch(error => {
console.log(error);
})
},
},
}
有什么想法吗?
更新
这是我保存的数据的返回方式
这是我根据Qonvex620 回答得到的结果
问题
提供Qonvex620 的答案我得到了这个问题
- 只有第一次保存的数据会附加,第二次等等。
返回
[Vue warn]: Missing required prop: "value" - 我还需要在追加中循环保存数据的子元素
当前代码
HTML
<div class="variationData">
<template v-for="(item, index) in savedVariations" >
<div :key="index">
<el-col :span="12" style="margin-top:15px;">
<!-- parent -->
<el-input :value="item.name" placeholder="Please input your variation name" class="input-with-select">
<el-select slot="prepend" placeholder="Select">
<el-option label="Text" :value="item.type"></el-option>
<el-option label="Text Area" :value="item.typerea"></el-option>
<el-option label="Boolean" :value="item.typean"></el-option>
</el-select>
</el-input>
</el-col>
<el-col class="line text-center" :span="3">Variation Value(s)</el-col>
<el-col :span="9" style="margin-top:15px;">
<!-- child's -->
<el-input :value="item.name" placeholder="Please input your variation value" class="input-with-select">
</el-input>
</el-col>
</div>
</template>
</div>
Script
data() {
return {
savedVariations: [],
}
},
methods: {
addVariants(e) {
e.preventDefault();
axios.post('/api/admin/variations/store', {
childs: this.variationChilds,
parent: this.variationParents,
})
.then(res => {
console.log(res);
this.isLoading = false;
//
this.savedVariations= res.data.data
//
})
.catch(error => {
console.log(error);
})
},
}
更新 2
我也设法循环了我孩子的数据,但仍然存在一个问题:如果我添加第二组数据将覆盖第一个附加数据,而不是在其下方/上方添加
代码
<div class="variationData">
<template v-for="(item, index) in savedVariations" >
<div :key="index">
<el-col :span="12" style="margin-top:15px;">
<!-- parent -->
<el-input :value="item.name" placeholder="Please input your variation name" class="input-with-select">
</el-input>
</el-col>
<el-col class="line text-center" :span="3">Variation Value(s)</el-col>
<el-col :span="9" style="margin-top:15px;">
<template v-for="(itemm, indexx) in item.children" >
<div :key="indexx">
<!-- child's -->
<el-input :value="itemm.name" placeholder="Please input your variation value" class="input-with-select">
</el-input>
</div>
</template>
</el-col>
</div>
</template>
</div>
想法?
更新 3 - 已解决
我的剩余问题(每次保存时多次附加“)使用此代码解决:
.then(res => {
this.savedVariations.push(res.data.data);
})
【问题讨论】:
-
vue 部分在哪里?
-
@Qonvex620 这个脚本在方法中
-
你的 div 变量数据也在 vue 中?
-
@Qonvex620 是的,它们都在同一个组件中,我已经更新了我的代码
-
为什么不在 axios 调用后更新你的状态并使用该更新来呈现你想要的部分,而不是使用 jQuery 来做到这一点?毕竟,这就是 Vue、React、Aurelia 等库的全部目的。只是我的两分钱。
标签: javascript vue.js