diff --git a/src/views/post/TagList.vue b/src/views/post/TagList.vue index 71d83849..082a5d09 100644 --- a/src/views/post/TagList.vue +++ b/src/views/post/TagList.vue @@ -9,7 +9,7 @@ :xs="24" :style="{ 'padding-bottom': '12px' }" > - + 保存 + + 更新 + 返回添加 + + + 删除 + @@ -50,9 +74,9 @@ {{ tag.postCount }} 篇文章 {{ tag.name }} @@ -67,9 +91,17 @@ import tagApi from '@/api/tag' export default { data() { return { + formType: 'create', tags: [], - tagToCreate: {}, - tagToUpdate: {} + tagToCreate: {} + } + }, + computed: { + title() { + if (this.tagToCreate.id) { + return '修改标签' + } + return '添加标签' } }, created() { @@ -81,21 +113,39 @@ export default { this.tags = response.data.data }) }, - handleCreateTag() { - tagApi.create(this.tagToCreate).then(response => { - this.loadTags() - }) + handleSaveClick() { + this.createOrUpdateTag() }, - handleUpdateTag(tagId) { - tagApi.update(tagId, this.tagToUpdate).then(response => { - this.loadTags() - }) + handleAddTag() { + this.formType = 'create' + this.tagToCreate = {} + }, + handleEditTag(tag) { + this.tagToCreate = tag + this.formType = 'update' }, handleDeleteTag(tagId) { tagApi.delete(tagId).then(response => { this.$message.success('删除成功!') this.loadTags() + this.handleAddTag() }) + }, + createOrUpdateTag() { + if (this.tagToCreate.id) { + tagApi.update(this.tagToCreate.id, this.tagToCreate).then(response => { + this.$message.success('更新成功!') + this.loadTags() + this.tagToCreate = {} + }) + } else { + tagApi.create(this.tagToCreate).then(response => { + this.$message.success('保存成功!') + this.loadTags() + this.tagToCreate = {} + }) + } + this.handleAddTag() } } }