vuejs2 - Vue onclick 显示特定项目
问题描述
我有一个关于 Vue 的问题。
我想为特定项目添加一个类:
<p v-on:click="display = !display">Rediger joke</p>
之前显示为假,将其更改为真。
它有效。但我的问题是,这个 onclick 在一个 v-for 循环中,我只想将“显示”放在一个“更新站点”上,而不是全部。我可以这样做还是必须尝试不同的设置?
非常感谢
解决方案
我有这个想法可能会对你有所帮助。post
这个想法是您使用例如属性扩展对象,visible
当您click event
触发时,您更改此属性并添加.display
类。请检查这个jsfiddle
模板
<div id="app">
<article v-for="post in filteredPosts" :key="post.id">
{{post.name}}
<button @click="display(post)">show</button>
<div class="post-content" :class="{display: post.visible}">this is the part I want to display onclick</div>
<hr />
</article>
</div>
css
.post-content {
display: none;
}
.post-content.display {
display: block;
}
代码
new Vue({
el: '#app',
data() {
return {
posts: []
};
},
created() {
//when you load posts. add visible property.
setTimeout(() => {
//posts from server
var postsFromServer = [{
id: 1,
name: 'Post One'
},
{
id: 2,
name: 'Post Two'
}
];
//add visible proprty.
this.posts = postsFromServer.map(post => {
return {
...post,
visible: false
};
});
}, 1000);
},
computed: {
filteredPosts() {
//do your filters
return this.posts;
}
},
methods: {
display(post) {
this.$set(post, 'visible', !post.visible);
}
}
});
推荐阅读
- python - 递归 Python 函数严重影响 Django 站点性能
- c - 如何根据条件将一个数组中的元素添加到另一个未定义大小的数组中?
- java - JSch 库 Kerberos 支持
- ios - iOS:如何在我的应用程序中更改 alertView 的每个文本、标题、按钮的每种字体?
- vb.net - 如何从 Dbnull 类型转换为 Date 类型
- angular - 如何在Angular 6中的Ngx-bootstrap模态中将组件作为模态体传递
- java - JComboBox 并不总是将值保存到 TableModel
- java - 在 Java 中使用 Javamail 获取消息内容类型时出错
- c - 结构定义缺失“()”的宏 - QAC 错误
- java - 无法解决的构建扩展:插件 org.apache.maven.wagon:wagon-webdav-jackrabbit:1.0-beta-6