搜索
在Vue.js中将数据和键推送到数组中
P粉564192131
P粉564192131 2024-02-25 14:22:51
[Vue.js讨论组]

我在 data() 中有一个数组:

data()  {
  return {
    list: [],
 }
},

methods: {
  pushData() {
     this.list.push({name:'yorn', age: 20});
  }
}

现在我想推送到以下格式的'list'数组,关键是info:

list [
     info [
     {
       name:yorn,
       age: 20
     }
  ]
 ]

我是 vuejs 和 javascript 新手,所以我需要大家的帮助。请给我你的意见。谢谢

P粉564192131
P粉564192131

全部回复(2)
P粉990008428

尝试更改 pushData 方法以具有 data 参数

pushData(data) {
 this.list.push(data);
}

调用方法

this.pushData({name: "john", age: 25});
P粉004287665

上述预期结果不是有效的 JSON。它应该像下面的

list: [{
    info: [{
        name: yorn,
        age: 20
    }]
}]

工作演示:

new Vue({
  el: '#app',
  data: {
    list: []
  },
  mounted() {
    this.pushData();
  },
  methods: {
    pushData() {
      this.list.push({info : [{name:'yorn', age: 20}] });
      // Or you can also use below one.
      // this.list[0].info.push({name:'yorn', age: 20});
    }
  }
})

{{ item.name }}

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号