我正在创建评论系统。我被封锁了因为我想在单击编辑后将段落更改为输入。 这是我的代码:
<section class="modal-card-body">
<p v-html="modalData.description" />
</section>
<section class="modal-card-body">
<article class="media">
<figure class="media-left">
<p class="image is-64x64">
<img src="https://bulma.io/images/placeholders/128x128.png">
</p>
</figure>
<div class="media-content">
<div class="content">
<strong>Barbara Middleton</strong>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis porta eros lacus, nec ultricies elit blandit non. Suspendisse pellentesque mauris sit amet dolor blandit rutrum. Nunc in tempus turpis.
<br>
<small> <a>Edit</a> · <a @click="openDeleteModal()">Usuń</a> · 3hrs</small>
</p>
</div>
</div>
</article>
<article class="media">
<figure class="media-left">
<p class="image is-64x64">
<img src="https://bulma.io/images/placeholders/128x128.png">
</p>
</figure>
<div class="media-content">
<form @submit.prevent="addComment">
<div class="field">
<p class="control">
<textarea v-model="contentComment" class="textarea" placeholder="Add a comment..." required />
</p>
</div>
<div class="field">
<p class="control">
<b-button
type="is-success"
rounded
native-type="submit"
>
Dodaj komentarz
</b-button>
</p>
</div>
</form>
</div>
</article>
</section>
我在注释部分的trello中找到了想要的效果。 有人可以帮我吗?
答案 0 :(得分:1)
您可以使用v-if
有条件地打开每个元素,以创建一个切换变量(或其他变量),该单击变量可以在单击时在内容和输入之间切换,为输入提供期望的初始值:
<input v-model="paragraghText"/>
...
paragraphText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis porta eros lacus, nec ultricies elit blandit non. Suspendisse pellentesque mauris sit amet dolor blandit rutrum. Nunc in tempus turpis."
答案 1 :(得分:1)
这是我的工作示例:
Vue.component("comment", {
template: "#comment",
data() {
return {
edit: false,
content: this.value
};
},
props: {
author: String,
value: String
},
methods: {
doEdit() {
this.edit = true;
},
doSave() {
this.edit = false;
this.$emit("input", this.content);
}
}
});
Vue.component("comments", {
template: "#comments",
data() {
return {
comments: [
{
id: 1,
author: "Fus",
message: "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
},
{
id: 2,
author: "Ro",
message: "Etiam nec sapien non arcu faucibus gravida at nec felis."
},
{
id: 3,
author: "Dah",
message:
"In ac felis libero. Morbi placerat sit amet nisi eu ultrices."
}
]
};
}
});
new Vue().$mount("#app");
.content,
.author {
margin: 5px;
max-width: 300px;
border: 1px dashed gray;
background-color: silver;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<comments></comments>
</div>
<script type="text/x-template" id="comments">
<section>
<comment v-for="item in comments" v-model="item.message" :author="item.author" :key="item.id"></comment>
</section>
</script>
<script type="text/x-template" id="comment">
<article>
<div class="author">Who: {{ author }}</div>
<div v-if="edit"><textarea v-model="content"></textarea></div>
<div v-else class="content">{{ value }}</div>
<button v-if="edit" @click="doSave">Save</button>
<button v-else @click="doEdit">Edit</button>
</article>
</script>