vue_动态渲染_添加obj属性_$set方法

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style type="text/css">
    .activated {color: red}
  </style>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
  <div id="app">
    <button @click="handleObjForeachload">对象遍历动态加载数据</button>
    <p>对象遍历</p>
    <template v-for="(value, key, index) of listObj" >
      <p>对象{{value}} --- {{key}} --- {{index}}</p>
      <p>数组{{arr[index]}}---- {{index}}</p>
    </template>
  </div>
</body>
<script type="text/javascript">
  let vm = new Vue({
    el: '#app',
    data: {
      listObj: {
        "goodsId": "0001",
        "goodsPrice": "¥5899.00",
        "goodsInfo": "直降500元!到手价5899元!享12期免息!低至16.2元/天",
        "goodsShop": "华为京东自营官方旗...",
        "goodsImg": "http://..............."
      },
      arr: ['nice', 'to', 'meet', 'you', 'too']
    },
    methods: {
      handleObjForeachload () {
        // set方法给对象动态添加属性
        this.$set(this.listObj, 'goodsAdress', 'beijing')
        //给对象同时添加多条属性
        this.listObj = Object.assign({}, this.listObj, {
          phone: '188888888',
          mail: '1111@qq.com'
        });
        // set方法给数组动态添加值//没有单独循环数组,添加同样多的数据
        this.$set(this.arr, this.arr.length, '!!!!!')
        this.$set(this.arr, this.arr.length, 'hello')
        this.$set(this.arr, this.arr.length, 'jack')

        console.log(this.listObj)
        console.log(this.arr)
      }
    }
  })
  
</script>
</html>
复制代码

 

posted @   前端之旅  阅读(3039)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示