代码干货 | vue.js列表渲染

news/2024/7/19 15:20:49 标签: js, 排序, 索引, index, key

本文来源于阿里云-云栖社区,原文点击这里。


v-for


我们用 v-for 指令根据一组数组的选项列表进行渲染。 v-for 指令需要以 item in items 形式的特殊语法, items 是源数据数组并且 item 是数组元素迭代的别名

基本用法

js django" style="margin:0px; padding:0.5em; font-size:undefined; font-family:Menlo,Monaco,Consolas,"Courier New",monospace; color:rgb(248,248,242); background:rgb(35,36,31); white-space:pre-wrap; display:block; overflow-x:auto">js-tag" style=""><js-name" style="color:rgb(102,217,239)">ul js-attr" style="">id=js-string" style="color:rgb(230,219,116)">"example-1">
    js-tag" style=""><js-name" style="color:rgb(102,217,239)">li js-attr" style="">v-for=js-string" style="color:rgb(230,219,116)">"item in items">js-template-variable" style="">{{ item.message }}js-tag" style=""></js-name" style="color:rgb(102,217,239)">li>
js-tag" style=""></js-name" style="color:rgb(102,217,239)">ul>
js-tag" style=""><js-name" style="color:rgb(102,217,239)">script>
js-keyword" style="color:rgb(249,38,114)">var example1 = js-keyword" style="color:rgb(249,38,114)">new Vue({
    el: js-string" style="color:rgb(230,219,116)">'#example-1',
    data: {
        items: [
            {message: js-string" style="color:rgb(230,219,116)">'Foo' },
            {message: js-string" style="color:rgb(230,219,116)">'Bar' }
        ]
    }
})
js-tag" style=""></js-name" style="color:rgb(102,217,239)">script>


在 v-for 块中,我们拥有对父作用域属性的完全访问权限。 v-for 还支持一个可选的第二个参数为当前项的索引

js django" style="margin:0px; padding:0.5em; font-size:undefined; font-family:Menlo,Monaco,Consolas,"Courier New",monospace; color:rgb(248,248,242); background:rgb(35,36,31); white-space:pre-wrap; display:block; overflow-x:auto">js-tag" style=""><js-name" style="color:rgb(102,217,239)">ul js-attr" style="">id=js-string" style="color:rgb(230,219,116)">"example-2">
    js-tag" style=""><js-name" style="color:rgb(102,217,239)">li js-attr" style="">v-for=js-string" style="color:rgb(230,219,116)">"(item, index) in items">
        js-template-variable" style="">{{ parentMessage }} - js-template-variable" style="">{{ index }} - js-template-variable" style="">{{ item.message }}
    js-tag" style=""></js-name" style="color:rgb(102,217,239)">li>
js-tag" style=""></js-name" style="color:rgb(102,217,239)">ul>
js-tag" style=""><js-name" style="color:rgb(102,217,239)">script>
js-keyword" style="color:rgb(249,38,114)">var example2 = js-keyword" style="color:rgb(249,38,114)">new Vue({
    el: js-string" style="color:rgb(230,219,116)">'#example-2',
    data: {
        parentMessage: js-string" style="color:rgb(230,219,116)">'Parent',
        items: [
            { message: js-string" style="color:rgb(230,219,116)">'Foo' },
            { message: js-string" style="color:rgb(230,219,116)">'Bar' }
        ]
    }
})
js-tag" style=""></js-name" style="color:rgb(102,217,239)">script>


你也可以用 of 替代 in 作为分隔符,因为它是最接近 JavaScript 迭代器的语法:

js applescript" style="margin:0px; padding:0.5em; font-size:undefined; font-family:Menlo,Monaco,Consolas,"Courier New",monospace; color:rgb(248,248,242); background:rgb(35,36,31); white-space:pre-wrap; display:block; overflow-x:auto"><js-keyword" style="color:rgb(249,38,114)">div v-js-keyword" style="color:rgb(249,38,114)">for=js-string" style="color:rgb(230,219,116)">"item of items"></js-keyword" style="color:rgb(249,38,114)">div>

Template v-for

如同 v-if 模板,你也可以用带有 v-for 的 标签来渲染多个元素块。


>>>展开全文


http://www.niftyadmin.cn/n/1452609.html

相关文章

inet_ntoa函数

将一个十进制网络字节序转换为点分十进制IP格式的字符串。

移动微社区----附手机端滑动分页源码

背景&#xff1a;楼主目前在一家做电子商务的公司上班&#xff0c;公司已有成熟的商城系统&#xff0c;社区系统&#xff0c;以及CMS框架。 而做社区的&#xff0c;最大的就是腾讯。今年大概三月份的时候&#xff0c;腾讯推出了微社区。推出了就推出了吧&#xff0c;这也没啥。…

HDU 1232 畅通工程 解题报告

题目http://acm.hdu.edu.cn/showproblem.php?pid1232 再看完了http://acshiryu.com/archives/559这篇非常有意思的并查集入门后&#xff0c;发现并查集非常好用以及好入手&#xff0c;于是这题完全仿照 我写的代码里没有运用路径压缩这一方法。 #include<stdio.h> #incl…

htons()函数

将主机的无符号短整形数转换成网络字节顺序。

Mysql大量插入数据时SQL语句的优化

1) 对于Myisam类型的表&#xff0c;可以通过以下方式快速的导入大量的数据。 ALTER TABLE tblname DISABLE KEYS; loading the data ALTER TABLE tblname ENABLE KEYS;这两个命令用来打开或者关闭Myisam表非唯一索引的更新。在导入大量的数据到一个非空的Myisam表时&…

技术文章 | 机器学习分析工具将如何驱动物联网

本文来源于阿里云-云栖社区&#xff0c;原文点击这里。 根据ABI Research近期的一份报告&#xff0c;机器学习分析工具将减少IoT的复杂性&#xff0c;并提高IoT的采用。该公司预计&#xff0c;随着机器学习即服务&#xff08;MLaaS&#xff09;模型的发展&#xff0c;机器学习数…

PL/SQL调用系统标准的请求实例

需求&#xff1a;实现供应商的接口导入&#xff1a; 导入供应商、供应商地址和供应商联系人的信息。 定义变量&#xff1a; --提交标准并发请求时的参数 l_request_id1 NUMBER; l_request_id2 NUMBER; l_request_id3 NUMBER; l_request_id4 NUMBER; l_bl_result…

Install Oracle10g On RedhatEL AS3 Update2 Step-by-Step (转)

虽然 windows 平台下的 oracle 已经装过几回了&#xff0c;但是 linux 下没有试过。看网上的文档 , 好像比 windows 下装要复杂不少&#xff0c;更改系统参数&#xff0c;创建 user&group, 检查必要的软件包&#xff0c;设置环境变量等一大堆工作要做。于是趁周末在VMWare中…