基于vue-cli li列表的显示隐藏
效果:点击“公告标题”,显示公告内容,点击同一个“公告标题”多次,显示与隐藏切换

方法一:
html部分代码:
<ul class="clist">
<li v-for="(item,index) in dlist" @click="listClick3(index)" :class="{selected: item.isturn}">
{{item.title}}
<div v-for="di in item.data" v-show="item.isturn" @click.stop="">{{di}}</div>
</li>
</ul>
js部分代码:
data () {
return {
dlist: [{
title: '公告标题1',data: ['公告内容1']
},{
title: '公告标题2',data: ['公告内容2']
},{
title: '公告标题3',data: ['公告内容3']
}
],
}
} ,
methods: {
listClick3: function(index){
var _title = this.dlist[index].title,
_data = this.dlist[index].data,
_isturn = !this.dlist[index].isturn;
this.dlist.splice(index,1,{title:_title,data:_data,isturn:_isturn});
},
}
css代码:
.clist li{
width: 100%;
line-height: 30px;
border: 1px solid #dddddd;
}
.clist li>div{
line-height: 30px;
}
.clist li.selected{
color: red;
}
这里有一个问题是为什么我在点击事件里写的方法是用splice,而不是直接this.dlist[index].isturn=!this.dlist[index].isturn呢,毕竟网上百度来的大部分都是这个样子写的,原因就是‘=’号不会触发双向数据绑定,因为我们在dlist声明的时候是数组不是变量,变量可以使用‘=’号,数组是不可以的哦
方法二:
html部分代码:
<ul class="clist">
<li v-for="(item,index) in dlist" @click="listClick2(index)" :class="{selected: showlist[index]}">
{{item.title}}
<div v-for="di in item.data" v-show="showlist[index]" @click.stop="">{{di}}</div>
</li>
</ul>
js部分代码:
data () {
return {
dlist: [{
title: '公告标题1',data: ['公告内容1']
},{
title: '公告标题2',data: ['公告内容2']
},{
title: '公告标题3',data: ['公告内容3']
}
],
showlist: []
}
},
created(){
for(var i=0;i<this.dlist.length;i++){
this.showlist.push(false);
}
},
methods:{
listClick2: function(index){
this.showlist.splice(index,1,!this.showlist[index]);
},
}
vue的官方文档有提供相应的api和方法:数组更新检测
根据上述api,使用$set方法进行数据的更改:
方法三:
html部分代码:
<ul class="clist">
<li v-for="(item,index) in dlist" @click="listClick4(index)" :class="{selected: item.isturn}">
{{item.title}}
<div v-for="di in item.data" v-show="item.isturn" @click.stop="">{{di}}</div>
</li>
</ul>
js部分代码:
data () {
return {
dlist: [{
title: '公告标题1',data: ['公告内容1']
},{
title: '公告标题2',data: ['公告内容2']
},{
title: '公告标题3',data: ['公告内容3']
}
],
}
},
created(){
for(var x in this.dlist){this.dlist.push(false);//不需要用到this.$set(this.dlist,x,false);created是在元素创建之前,那个时候随便改动都可以。不需要用set通知vue
}
},
methods: {
listClick4: function(index){
this.dlist[index].isturn = !this.dlist[index].isturn;
this.$set(this.dlist,index,this.dlist[index]);
},
}
方法四:
html部分代码:
<ul class="clist">
<li v-for="(item,index) in dlist" @click="listClick5(index)" :class="{selected: showlist[index]}">
{{item.title}}
<div v-for="di in item.data" v-show="showlist[index]" @click.stop="">{{di}}</div>
</li>
</ul>
javascript部分代码:
data () {
return {
dlist: [{
title: '公告标题1',data: ['公告内容1']
},{
title: '公告标题2',data: ['公告内容2']
},{
title: '公告标题3',data: ['公告内容3']
}
],
}
},
created(){
for(var x in this.dlist){
this.showlist.push(false);
}
},
methods:{
listClick5: function(index){
this.$set(this.showlist,index,!this.showlist[index]);
},
}
基于vue-cli li列表的显示隐藏的更多相关文章
- 前端基于VUE的v-charts的曲线显示
目录 前端基于VUE的v-charts的曲线显示 1. 应用背景 2. 分析数据生产者生成 3. 取出数据消费者 4. 前端显示 4.1 安装V-charts插件 4.2 引入veline曲线插件 4 ...
- jQuery入门——实现列表的显示隐藏与实现轮播图
列表的显示与隐藏 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head& ...
- 基于jquery封装通用的控制显示隐藏的方法
应用场景 在项目中会存在大量这样的需求: 1.选择不同的radio单选框,页面上的部分内容随之显示隐藏 2.选择不同的option下拉框内容,页面上的部分内容随之显示隐藏 如果每次遇到这类需求都单独写 ...
- 基于vue cli 3.0创建前端项目并安装cube-ui
前提条件: 安装node.js. 国内的开发者最好先配置淘宝镜像. 之后用cnpm来代替npm命令. 项目创建过程: 打开cmd,输入命令进入目标工作空间,以本机项目为例: cd /d d: cd D ...
- 基于@vue/cli 的构建项目(3.0)
1.检测node的版本号 注意:1.Vue CLI需要Node.js的版本 8.9+(推荐8.11.0+) 所以在安装Vue CLI之前先看下node的版本 node -v 2.安装@vue/cli ...
- 基于Vue cli生成的Vue项目的webpack4升级
前面的话 本文将详细介绍从webpack3到webpack4的升级过程 概述 相比于webpack3,webpack4可以零配置运行,打包速度比之前提高了90%,可以直接到ES6的代码进行无用代码剔除 ...
- 基于vue的滚动条组件之--element隐藏组件滚动条scrollbar使用
在项目中,总是需要用到滚动条,但windows浏览器默认的滚动条是很丑的,为了页面美观,可以考虑优化滚动条样式. vue Element UI官方文档上并没有放出滚动条相关的示例说明,但是实际上是有 ...
- Vue CLI 3+tinymce 5富文本编辑器整合
基于Vue CLI 3脚手架搭建的项目整合tinymce 5富文本编辑器,vue cli 2版本及tinymce 4版本参考:https://blog.csdn.net/liub37/article/ ...
- 基于vue现有项目的服务器端渲染SSR改造
前面的话 不论是官网教程,还是官方DEMO,都是从0开始的服务端渲染配置.对于现有项目的服务器端渲染SSR改造,特别是基于vue cli生成的项目,没有特别提及.本文就小火柴的前端小站这个前台项目进行 ...
随机推荐
- laravel @if
- [JSON] Validating/Asserting JSON response with Jsonlurper
import groovy.json.JsonSlurper def response = messageExchange.response.responseContent log.info &quo ...
- BCompare 4重置试用天数
BCompare安装后有30天试用期,试用结束后,你可以卸载重装,以重新获得30天试用天数. BCompare的使用天数记录保存在注册表中,如果不想每次重装,也可删除对应的注册表值来重置激活天数. 命 ...
- Oracle 11g 重建EM需要删除的对象
因为需求需要重建EM,重建时因为某些错误被迫停止,比如对象已存在.用户已经存在等,最终找出了创建必备的条件: 1.环境变量(Oracle和Grid在同一个用户下安装) ORACLE_HOME 要设为D ...
- Integer中getInteger(),valueof()
Integer类有两个看起来很类似的静态方法,一个是Integer.getInteger(String),另外一个是Integer.valueOf(String).如果只看方法名称的话,很容易将这两个 ...
- python切片、列表解析、元组
1.列表解析 test = [x**2 for x in range(1,11)] 2.切片 test1 = ["a","b","c",&q ...
- Word文档发布到CSDN博客
目前大部分的博客作者在写博客这件事情上都会遇到以下3个痛点:1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.2.发布到博客或公众号平台 ...
- C# 单例模式(Singleton)
摘要 在我们日常的工作中经常需要在应用程序中保持一个唯一的实例,如:IO处理,数据库操作等,由于这些对象都要占用重要的系统资源,所以我们必须限制这些实例的创建或始终使用一个公用的实例,这就是我们今天要 ...
- Delphi 10.1.2 berlin开发跨平台APP的几点经验
1.ios不允许app有退出功能,所以不能调用Application.Terminate. 2.info.plist文件的自定义:info.plist文件是由info.plist.TemplateiO ...
- Verilog MIPS32 CPU(八)-- 控制器
Verilog MIPS32 CPU(一)-- PC寄存器 Verilog MIPS32 CPU(二)-- Regfiles Verilog MIPS32 CPU(三)-- ALU Verilog M ...