【发布时间】:2019-12-24 23:49:28
【问题描述】:
我有一个应用程序,用户可以在其中手动删除帖子中的文本和文件附件。它工作正常,用户可以删除帖子中的文本和附件,但是,问题是当用户选择“取消”按钮时——附件仍然显示已编辑的附件项目。期望的结果是附件恢复到原始列表。
有人对我如何解决这个问题有任何建议吗?
我在这里设置了一个演示:Codesandbox(点击 POSTS --> POST ID --> EDIT POST --> 删除附件 --> CANCEL EDIT )
我正在使用 props 将数据传递给子组件:
名为
PostDetail.vue的父组件:
模板:
<section v-if="editPostFormIsVis">
<EditPost
:post="post"
@update="editPostFormIsVis=false"
@cancel="editPostFormIsVis = false"
:attachmentsArray="attachmentsArray"
@deleteMediaAttachment="removeItem"
/>
</section>
脚本:
import attachments from "../../public/attachments.json";
import EditPost from "@/components/EditPost.vue";
export default {
components: {
EditPost
},
data() {
return {
post: {},
editPostFormIsVis: false,
attachmentsArray: attachments
};
},
created() {
this.getPost();
},
methods: {
getPost() {
axios
.get(
"https://jsonplaceholder.typicode.com/posts/" + this.$route.params.id
)
.then(resp => {
this.post = resp.data;
})
.catch(err => {
console.log(err);
});
},
editPost() {
this.editPostFormIsVis = true;
},
removeItem: function(item) {
this.attachmentsArray.attachments.splice(item, 1);
}
},
computed: {
emailAttachmentsFileNames() {
if (this.attachmentsArray.emailAttachments) {
const emailAttachmentsFileNameArray = this.attachmentsArray.emailAttachments.map(
item => {
const tokens = item.split("/");
return tokens[tokens.length - 1];
}
);
return emailAttachmentsFileNameArray;
} else {
return null;
}
},
attachmentsFileNames() {
if (this.attachmentsArray.attachments) {
const attachmentsFileNameArray = this.attachmentsArray.attachments.map(
item => {
const tokens = item.split("/");
return tokens[tokens.length - 1];
}
);
return attachmentsFileNameArray;
} else {
return null;
}
}
}
};
名为
EditPost.vue的子组件:
模板:
<ul>
<li>Media Attachments
<ul v-if="attachmentsFileNames && attachmentsFileNames.length">
<li v-for="(attachmentFileName, index) in attachmentsFileNames" :key="index">
<a href="
#
">{{ attachmentFileName }}</a>
<button
@click.prevent="$emit('deleteMediaAttachment', attachmentFileName)"
>Delete me!</button>
</li>
</ul>
</li>
</ul>
脚本:
export default {
name: "EditPost",
props: {
post: {
type: Object
},
attachmentsArray: {
type: Object
}
// filenames: {
// type: Array
// }
},
created() {
// clone issue without including observers and reactivity
this.postBeforeEdit = Object.assign({}, this.post);
this.attachmentsArrayBeforeEdit = Object.assign({}, this.attachmentsArray);
//this.attachmentsFileNamesBeforeEdit = Object.assign({}, this.attachmentsFileNames)
},
methods: {
editPost() {
axios
.put("https://jsonplaceholder.typicode.com/posts/" + this.post.id)
.then(response => {
//dosomething
console.log("server response: ", response.status);
})
.then(() => {
this.$emit("update");
})
.catch(err => {
console.log(err);
});
},
cancelEdit() {
// revert UI back to original issue values if user cancels edits
Object.assign(this.post, this.postBeforeEdit);
Object.assign(this.attachmentsArray, this.attachmentsArrayBeforeEdit);
// Object.assign(this.attachmentsFileNames, this.attachmentsFileNamesBeforeEdit);
this.$emit("cancel");
}
},
computed: {
emailAttachmentsFileNames() {
if (this.attachmentsArray.emailAttachments) {
const emailAttachmentsFileNameArray = this.attachmentsArray.emailAttachments.map(
item => {
const tokens = item.split("/");
return tokens[tokens.length - 1];
}
);
return emailAttachmentsFileNameArray;
} else {
return null;
}
},
attachmentsFileNames() {
if (this.attachmentsArray.attachments) {
const attachmentsFileNameArray = this.attachmentsArray.attachments.map(
item => {
const tokens = item.split("/");
return tokens[tokens.length - 1];
}
);
return attachmentsFileNameArray;
} else {
return null;
}
}
}
};
知道为什么取消按钮不会将附件数组恢复为原始吗?
【问题讨论】:
标签: javascript vuejs2 vue-cli-3