Vue响应式添加、修改数组和对象的值

前端之家收集整理的这篇文章主要介绍了Vue响应式添加、修改数组和对象的值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

有些时候,不得不想添加修改数组和对象的值,但是直接添加修改后又失去了getter、setter。

由于 JavaScript 的限制, Vue 不能检测以下变动的数组:

1. 利用索引直接设置一个项时,例如: vm.items[indexOfItem] = newValue

2. 修改数组的长度时,例如: vm.items.length = newLength

为了避免第一种情况,以下两种方式将达到像 vm.items[indexOfItem] = newValue 的效果, 同时也将触发状态更新:

避免第二种情况,使用 splice:

Vue 不允许在已经创建的实例上动态添加新的根级响应式属性(root-level reactive property)。然而它可以使用Vue.set(object,key,value) 方法将响应属性添加到嵌套的对象上:

您还可以使用 vm.$set 实例方法,这也是全局 Vue.set 方法的别名:

有时你想向已有对象上添加一些属性,例如使用 object.assign() 或 _.extend() 方法添加属性。但是,添加到对象上的新属性不会触发更新。在这种情况下可以创建一个新的对象,让它包含原对象的属性和新的属性

示例代码:

<Meta charset="utf-8"> @H_<a href="/tag/403/" target="_blank" class="keywords">403</a>_33@ </head> <body> <p><div id="app"></p> <ol> <li v-for="data in list">{{data.id}} - {{data.name}}</li> </ol> </div> <p><script type="text/javascript" src="../under<a href="/tag/score/" target="_blank" class="keywords">score</a>/under<a href="/tag/score/" target="_blank" class="keywords">score</a>-min.js"></script><br /> <script type="text/javascript" src="./vue.2.1.8.min.js"></script><br /> <script type="text/javascript"><br /> var oVue = new Vue({<br /> el : '#app',data : {<br /> list : [<br /> {id : 5},{id : 6}<br /> ]<br /> }<br /> });</p> </script> </body> </html></pre> </div> <p style="text-align: center"><p class="pic_center"><img src="https://files.jb51.cc/file_images/article/201703/201703210841121.png" /></p></p> <p>以上就是VueI<a href="/tag/tianjia/" target="_blank" class="keywords">添加</a><a href="/tag/xiugai/" target="_blank" class="keywords">修改</a>数组与对象值时同时将触发状态更新的<a href="/tag/fangfa/" target="_blank" class="keywords">方法</a>,希望本文的<a href="/tag/neirong/" target="_blank" class="keywords">内容</a>对大家的学习或者工作能带来一定的帮助,同时也希望多多<a href="/tag/zhichi/" target="_blank" class="keywords">支持</a>编程之家!</p></div> <div class="topcard-tags"><a href="/tag/vue/" class="tag_link" target="_blank">vue</a><a href="/tag/pvue/" class="tag_link" target="_blank">vue</a><a href="/tag/vuep/" class="tag_link" target="_blank">vue</a><a href="/tag/xiugaishuzu/" class="tag_link" target="_blank">修改数组</a><a href="/tag/duixiangshuzu/" class="tag_link" target="_blank">对象数组</a><a href="/tag/tianjiashuzu/" class="tag_link" target="_blank">添加数组</a></div> <ul class="list-group"> <li class="list-group-item"><a href="/vue/40575.html" title="Vue指令的钩子函数使用方法">上一篇:Vue指令的钩子函数使用方法</a><a href="/vue/40557.html" title="详解Vue 实例中的生命周期钩子" class="text-muted pull-right">下一篇:详解Vue 实例中的生命周期钩子</a> </li> </ul> </div> </div> </div> <!-- row end --> <div class="row row-sm"> <div class="col-sm-12 col-md-12 col-lg-12"> <div class="card"> <ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-4605373693034661" data-ad-slot="9144498553"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script></div> </div> </div> <div class="row row-sm"> <div class="col-sm-12 col-md-12 col-lg-12"> <div class="card"> <div class="title"><h1>猜你在找的Vue相关文章</h1></div> <div class="list_con"> <a href="/vue/997429.html" title="elementui的el-tree第一次加载无法展开和选中的问题"><div class="title">elementui的el-tree第一次加载无法展开和选中的问题</div> <div class="summary">问题现象 elmentui的el-tree数据加载问题,导致第一次加载选中当前节点和高亮当前节点没有...</div> <time class="summary">作者:前端之家 时间:2021-02-17</time> </a> </div> <div class="list_con"> <a href="/vue/997428.html" title="VSCODE打开一个文件,另一个文件就关闭的问题的解决方法"><img class="lazy" src="/images/np.jpg" data-original="/res/2021/02-17/21/9e2e32f4ad7f00fdffa540c0f7d918e1.png" title="" width="160" height="90" style="float:right;margin-left:30px;display:none;" /><div class="title">VSCODE打开一个文件,另一个文件就关闭的问题的解决方法</div> <div class="summary">因为刚打开文件,vscode默认是预览状态,如果编辑过之后,就不会有这个问题。 可以通过双击...</div> <time class="summary">作者:前端之家 时间:2021-02-17</time> </a> </div> <div class="list_con"> <a href="/vue/997427.html" title="一文带你学会国产加密算法SM4的vue实现方案"><img class="lazy" src="/images/np.jpg" data-original="/res/2021/02-17/21/29a0275a80b1fac147aa687624a9bfe1.png" title="" width="160" height="90" style="float:right;margin-left:30px;display:none;" /><div class="title">一文带你学会国产加密算法SM4的vue实现方案</div> <div class="summary">前言 上篇文章我们介绍了国产SM4加密算法的后端java实现方案。没有看过的小伙伴可以看一下...</div> <time class="summary">作者:前端之家 时间:2021-02-17</time> </a> </div> <div class="list_con"> <a href="/vue/997426.html" title="解决vue报错:Module build failed (from ./node_modules/_eslint-loader@2.2.1@eslint-loader/index.js): TypeError: Cannot read property 'range' of null"><img class="lazy" src="/images/np.jpg" data-original="/res/2021/02-17/21/ac433ae643be89ef37b127b24fa2a61a.png" title="" width="160" height="90" style="float:right;margin-left:30px;display:none;" /><div class="title">解决vue报错:Module build failed (from ./node_modules/_eslint-loader@2.2.1@eslint-loader/index.js): TypeError: Cannot read property 'range' of null</div> <div class="summary">在项目中引入动态路由时报错 写法: 报错: Module build failed (from ./node_modules/_esl...</div> <time class="summary">作者:前端之家 时间:2021-02-17</time> </a> </div> <div class="list_con"> <a href="/vue/997425.html" title="vue中解决Uncaught ReferenceError: regeneratorRuntime is not defined问题"><img class="lazy" src="/images/np.jpg" data-original="/res/2021/02-17/21/9f2045768979b899c69e66354a1f62d0.png" title="" width="160" height="90" style="float:right;margin-left:30px;display:none;" /><div class="title">vue中解决Uncaught ReferenceError: regeneratorRuntime is not defined问题</div> <div class="summary">问题产生 在使用babel编译es6时,遇到报错Uncaught ReferenceError: regeneratorRuntime i...</div> <time class="summary">作者:前端之家 时间:2021-02-17</time> </a> </div> <div style="border-bottom: 1px solid #f4f4f4;margin-top:20px;"> <ins class="adsbygoogle" style="display:block" data-ad-format="fluid" data-ad-layout-key="-fr-2o+fp-dx-wx" data-ad-client="ca-pub-4605373693034661" data-ad-slot="4561116489"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div><div class="list_con"> <a href="/vue/997423.html" title="element-UI中手动调用table排序"><div class="title">element-UI中手动调用table排序</div> <div class="summary">&amp;lt;el-table :data=&amp;quot;tableData&amp;quot; &amp;quot;width...</div> <time class="summary">作者:前端之家 时间:2021-02-17</time> </a> </div> <div class="list_con"> <a href="/vue/994895.html" title="Vue双向绑定原理及其实现"><img class="lazy" src="/images/np.jpg" data-original="/res/2021/01-10/13/13055abb3c90b13ed50222b980d4a858.gif" title="" width="160" height="90" style="float:right;margin-left:30px;display:none;" /><div class="title">Vue双向绑定原理及其实现</div> <div class="summary">在之前面试的时候被面试官问到是否了解Vue双向绑定的原理,其实自己之前看过双向绑定的原理...</div> <time class="summary">作者:前端之家 时间:2021-01-10</time> </a> </div> <div class="list_con"> <a href="/vue/994894.html" title="Vue事件修饰符详解"><img class="lazy" src="/images/np.jpg" data-original="/res/2021/01-10/13/b4cf1534468c90981806f6f883c8e876.png" title="" width="160" height="90" style="float:right;margin-left:30px;display:none;" /><div class="title">Vue事件修饰符详解</div> <div class="summary">整体学习Vue时看到Vue文档中有事件修饰符的描述,但是看了之后并没有理解是什么意思,于是...</div> <time class="summary">作者:前端之家 时间:2021-01-10</time> </a> </div> <div class="list_con"> <a href="/vue/994741.html" title="Vue-router插件使用"><img class="lazy" src="/images/np.jpg" data-original="/res/2021/01-01/21/bf84b4bbaaac53ea67ceec4898781512.png" title="" width="160" height="90" style="float:right;margin-left:30px;display:none;" /><div class="title">Vue-router插件使用</div> <div class="summary">单页面原理 Vue是单页面开发,即页面不刷新。 页面不刷新,而又要根据用户选择完成内容的更...</div> <time class="summary">作者:前端之家 时间:2021-01-01</time> </a> </div> <div class="list_con"> <a href="/vue/994740.html" title="Vue 分支循环"><img class="lazy" src="/images/np.jpg" data-original="/res/2021/01-01/21/ed0d5c51fe9891d0c42f795f23333ec1.png" title="" width="160" height="90" style="float:right;margin-left:30px;display:none;" /><div class="title">Vue 分支循环</div> <div class="summary">v-for 通过v-for进行循环,不光可以拿到元素本身,也可以拿到索引值。 如果数据是对象类型...</div> <time class="summary">作者:前端之家 时间:2021-01-01</time> </a> </div> <div style="border-bottom: 1px solid #f4f4f4;margin-top:20px;"> <ins class="adsbygoogle" style="display:block" data-ad-format="fluid" data-ad-layout-key="-fr-2o+fp-dx-wx" data-ad-client="ca-pub-4605373693034661" data-ad-slot="4561116489"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div></div> </div> </div> </div> <!-- left end--> <!-- right --> <div class="col-sm-12 col-md-12 col-lg-3"> <!-- row --> <div class="row row-sm"> <div class="col-sm-12 col-md-12 col-lg-12"> <div class="card"> <label class="main-content-label ">编程分类</label> <div class="cate mt-20"><a href="/html/" title="HTML">HTML</a><a href="/html5/" title="HTML5">HTML5</a><a href="/js/" title="JavaScript">JavaScript</a><a href="/css/" title="CSS">CSS</a><a href="/jquery/" title="jQuery">jQuery</a><a href="/bootstrap/" title="Bootstrap">Bootstrap</a><a href="/angularjs/" title="Angularjs">Angularjs</a><a href="/typescript/" title="TypeScript">TypeScript</a><a href="/vue/" title="Vue">Vue</a><a href="/dojo/" title="Dojo">Dojo</a><a href="/json/" title="Json">Json</a><a href="/electron/" title="Electron">Electron</a><a href="/nodejs/" title="Node.js">Node.js</a><a href="/extjs/" title="extjs">extjs</a><a href="/express/" title="Express ">Express </a><a href="/xml/" title="XML">XML</a><a href="/es6/" title="ES6">ES6</a><a href="/ajax/" title="Ajax">Ajax</a><a href="/flash/" title="Flash">Flash</a><a href="/unity/" title="Unity">Unity</a><a href="/react/" title="React">React</a><a href="/flex/" title="Flex">Flex</a><a href="/antdesign/" title="Ant Design">Ant Design</a><a href="/webfrontend/" title="Web前端">Web前端</a><a href="/weapp/" title="微信小程序">微信小程序</a><a href="/wxmp/" title="微信公众号">微信公众号</a><div class="clearfix"></div> </div> </div> </div> </div> <!-- row end --> <!-- row --> <div class="row row-sm"> <div class="col-sm-12 col-md-12 col-lg-12"> <div class="card"> <!-- f2er-rightads --> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-4605373693034661" data-ad-slot="7756441254" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> </div> <!-- row end --> <!-- row --> <div class="row row-sm"> <div class="col-sm-12 col-md-12 col-lg-12"> <div class="card"> <label class="main-content-label ">最新文章</label> <ul class="n-list"><li><a href="/vue/997429.html" title="elementui的el-tree第一次加载无法展开和选中的问题" target="_blank">• elementui的el-tree第一次</a></li> <li><a href="/vue/997428.html" title="VSCODE打开一个文件,另一个文件就关闭的问题的解决方法" target="_blank">• VSCODE打开一个文件,另一</a></li> <li><a href="/vue/997427.html" title="一文带你学会国产加密算法SM4的vue实现方案" target="_blank">• 一文带你学会国产加密算法</a></li> <li><a href="/vue/997426.html" title="解决vue报错:Module build failed (from ./node_modules/_eslint-loader@2.2.1@eslint-loader/index.js): TypeError: Cannot read property 'range' of null" target="_blank">• 解决vue报错:Module buil</a></li> <li><a href="/vue/997425.html" title="vue中解决Uncaught ReferenceError: regeneratorRuntime is not defined问题" target="_blank">• vue中解决Uncaught Refere</a></li> <li><a href="/vue/997424.html" title="vue的自定义组件如何使用prop传值?" target="_blank">• vue的自定义组件如何使用p</a></li> <li><a href="/vue/997423.html" title="element-UI中手动调用table排序" target="_blank">• element-UI中手动调用tabl</a></li> <li><a href="/vue/994895.html" title="Vue双向绑定原理及其实现" target="_blank">• Vue双向绑定原理及其实现</a></li> <li><a href="/vue/994894.html" title="Vue事件修饰符详解" target="_blank">• Vue事件修饰符详解</a></li> <li><a href="/vue/994741.html" title="Vue-router插件使用" target="_blank">• Vue-router插件使用</a></li> </ul> </div> </div> </div> <!-- row end --> <!-- row --> <div class="row row-sm"> <div class="col-sm-12 col-md-12 col-lg-12"> <div class="card"> <label class="main-content-label ">热门标签 <span class="pull-right tx-12"> <a href="/all" target="_blank">更多 ►</a></span> </label> <div class="topcard-tags"><a href="/tag/guanbiyangao/" title="关闭广告" target="_blank">关闭广告</a><a href="/tag/danduheaders/" title="单独headers" target="_blank">单独headers</a><a href="/tag/fengzhuangdaima/" title="封装代码" target="_blank">封装代码</a><a href="/tag/tishicuowu/" title="提示错误" target="_blank">提示错误</a><a href="/tag/zhengshuzhengze/" title="整数正则" target="_blank">整数正则</a><a href="/tag/fei0kaitou/" title="非0开头" target="_blank">非0开头</a><a href="/tag/tiaoye/" title="跳页" target="_blank">跳页</a><a href="/tag/chuyema/" title="出页码" target="_blank">出页码</a><a href="/tag/antdtable/" title="antd table" target="_blank">antd table</a><a href="/tag/tishiURLweizhuce/" title="提示URL未注册" target="_blank">提示URL未注册</a><a href="/tag/gongzhonghaozhifu/" title="公众号支付" target="_blank">公众号支付</a><a href="/tag/vuehashmoshi/" title="vue hash模式" target="_blank">vue hash模式</a><a href="/tag/iSlider/" title="iSlider" target="_blank">iSlider</a><a href="/tag/chepaijianpan/" title="车牌键盘" target="_blank">车牌键盘</a><a href="/tag/xunhuantupian/" title="循环图片" target="_blank">循环图片</a><a href="/tag/echartsshuangzhexian/" title="echarts 双折线" target="_blank">echarts 双折</a><a href="/tag/zuoyoubuju/" title="左右布局" target="_blank">左右布局</a><a href="/tag/DllPlugin/" title="DllPlugin" target="_blank">DllPlugin</a><a href="/tag/duixiangchuangjian/" title="对象创建" target="_blank">对象创建</a><a href="/tag/daziyouxi/" title="打字游戏" target="_blank">打字游戏</a><a href="/tag/quanxuan/" title="圈选" target="_blank">圈选</a><a href="/tag/lianglan/" title="两栏" target="_blank">两栏</a><a href="/tag/yunhanshu/" title="云函数" target="_blank">云函数</a><a href="/tag/mengban/" title="蒙版" target="_blank">蒙版</a><a href="/tag/ES2020/" title="ES2020" target="_blank">ES2020</a><a href="/tag/chuchuang/" title="橱窗" target="_blank">橱窗</a><a href="/tag/wufenggundonglunbo/" title="无缝滚动轮播" target="_blank">无缝滚动轮播</a><a href="/tag/sekuaipengzhuang/" title="色块碰撞" target="_blank">色块碰撞</a><a href="/tag/zujianxiaohui/" title="组件销毁" target="_blank">组件销毁</a><a href="/tag/wendangcaozuo/" title="文档操作" target="_blank">文档操作</a></div> </div> </div> </div> <!-- row end --> <!-- row --> <div class="row row-sm"> <div class="col-sm-12 col-md-12 col-lg-12"> <div class="card"> <!-- f2er-rightads --> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-4605373693034661" data-ad-slot="7756441254" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> </div> <!-- row end --> </div> <!-- right end --> </div> </div> <footer id="footer"> <div class="container"> <div class="row hidden-xs"> <dl class="col-sm-6 site-link"> <dt>最近更新</dt><dd><a href="/win11/1005328.html" title="小米手机重装系统价格多少?专业维修服务详解" target="_blank">· 小米手机重装系统价格多少?专业维修服务详解</a><span class="text-muted pull-right">02-05</span></dd> <dd><a href="/win11/1005327.html" title="手把手教你重装电脑系统,让你的电脑焕然一新!" target="_blank">· 手把手教你重装电脑系统,让你的电脑焕然一新!</a><span class="text-muted pull-right">02-05</span></dd> <dd><a href="/win11/1005326.html" title="教你一步步重装XP系统,让你的电脑重获新生" target="_blank">· 教你一步步重装XP系统,让你的电脑重获新生</a><span class="text-muted pull-right">02-05</span></dd> <dd><a href="/win11/1005325.html" title="从备份到上网:一步步教你重装电脑系统" target="_blank">· 从备份到上网:一步步教你重装电脑系统</a><span class="text-muted pull-right">02-05</span></dd> <dd><a href="/win11/1005324.html" title="Sony笔记本电脑一键重装系统详细图文教程" target="_blank">· Sony笔记本电脑一键重装系统详细图文教程</a><span class="text-muted pull-right">02-05</span></dd> <dd><a href="/win11/1005323.html" title="Lenovo笔记本重装系统超详细教程,小白也能轻松上手" target="_blank">· Lenovo笔记本重装系统超详细教程,小白也能轻松...</a><span class="text-muted pull-right">02-05</span></dd> <dd><a href="/win11/1005322.html" title="联想笔记本一键重装Win10系统详细教程" target="_blank">· 联想笔记本一键重装Win10系统详细教程</a><span class="text-muted pull-right">02-05</span></dd> <dd><a href="/win11/1005321.html" title="电脑系统故障无需愁,专业维修店帮你重装旧貌换新颜" target="_blank">· 电脑系统故障无需愁,专业维修店帮你重装旧貌换新...</a><span class="text-muted pull-right">02-05</span></dd> <dd><a href="/win11/1005320.html" title="联想笔记本一键重装Win10系统图文教程,小白也能轻松搞定!" target="_blank">· 联想笔记本一键重装Win10系统图文教程,小白也能...</a><span class="text-muted pull-right">02-05</span></dd> <dd><a href="/win11/1005319.html" title="笔记本重装系统图文教程:从光盘启动一步到位" target="_blank">· 笔记本重装系统图文教程:从光盘启动一步到位</a><span class="text-muted pull-right">02-05</span></dd> </dl> <dl class="col-sm-4 site-link"> <dt>好站推荐</dt><dd> <a href="https://www.runoob.com" title="菜鸟教程(www.runoob.com)提供了编程的基础技术教程, 介绍了HTML、CSS、Javascript、Python,Java,Ruby,C,PHP , MySQL等各种编程语言的基础知识。 同时本站中也提供了大量的在线实例,通过实例,您可以更好的学习编程。" target="_blank">菜鸟教程</a></dd><dd> <a href="https://www.jb51.cc" title="编程之家(www.jb51.cc)是成立于2017年面向全球中文开发者的技术内容分享平台。提供编程导航、编程问答、编程博文、编程百科、编程教程、编程工具、编程实例等开发者最需要的编程技术内容与开发工具支持,与你一起学习编程,相信编程改变未来!" target="_blank">编程之家</a></dd><dd> <a href="" title="前端之家 f2er.com 前端开发人员所需学习知识手册。" target="_blank">前端之家</a></dd></dl> <dl class="col-sm-2 site-link"> <dt>商务合作</dt> <dd><a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=76874919&site=qq&menu=yes">联系我们</a></dd> </dl> </div> <div class="copyright"> Copyright © 2019 前端之家. 当前版本 V7.0.16<br> <span class="ml5">前端之家 版权所有 <a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">闽ICP备13020303号-10</a></span> </div> </div> </footer> <script type="text/javascript" src="/js/base.js"></script> </body> </html>