Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Vue.js尝试在添加新行时增加数组值_Javascript_Node.js_Ecmascript 6_Vue.js - Fatal编程技术网

Javascript Vue.js尝试在添加新行时增加数组值

Javascript Vue.js尝试在添加新行时增加数组值,javascript,node.js,ecmascript-6,vue.js,Javascript,Node.js,Ecmascript 6,Vue.js,我试图在Vue.js中添加新行时增加数组值,但出现错误: 组件渲染函数中可能有一个无限更新循环 JavaScript: new Vue({ el: '#app', data: () => ({ inputs: [{ id: 0, username: '' }], }), methods: { add() { this.inputs.push({ id: this.id++, username: '' });

我试图在Vue.js中添加新行时增加数组值,但出现错误:

组件渲染函数中可能有一个无限更新循环

JavaScript:

new Vue({
  el: '#app',
  data: () => ({
    inputs: [{
      id: 0,
      username: ''
    }],
  }),
  methods: {
    add() {
      this.inputs.push({ id: this.id++, username: '' });
    }
  }
});
标记:

<div id="app">
  <div v-for="input in inputs">
    <input :key="input.id" :name="'username_' + input.id" type="text" v- v-model="input.username" placeholder="username">
  </div>
  <button @click="add">Add</button>
</div>

那么,我怎样才能让它充满活力呢?键入
id:this.id++
无效。

问题:-this.id未定义,Vue不知道id,因为它不在组件数据中

所以你必须选择解决问题的方法

  • 像这样定义变量id

    data(){
         id:0,inputs:[]
     }
    
  • 解决这个问题的另一个选择是替换这行代码

        this.inputs.push({ id: this.id++, username: '' });
    
    用这条线

      this.inputs.push({ id: this.inputs.length, username: '' });
    
    这将解决问题,这是完整的代码

newvue({
el:“#应用程序”,
数据:()=>({
投入:[{
id:0,
用户名:“”
}],
}),
方法:{
加(){
console.log(this.inputs.length);
this.inputs.push({id:this.inputs.length,用户名:“”});
}
}
});

添加

问题:-此.id未定义,Vue不知道id,因为它不在组件数据中

所以你必须选择解决问题的方法

  • 像这样定义变量id

    data(){
         id:0,inputs:[]
     }
    
  • 解决这个问题的另一个选择是替换这行代码

        this.inputs.push({ id: this.id++, username: '' });
    
    用这条线

      this.inputs.push({ id: this.inputs.length, username: '' });
    
    这将解决问题,这是完整的代码

newvue({
el:“#应用程序”,
数据:()=>({
投入:[{
id:0,
用户名:“”
}],
}),
方法:{
加(){
console.log(this.inputs.length);
this.inputs.push({id:this.inputs.length,用户名:“”});
}
}
});

添加
更简单地说:

使用索引。为我工作

享受吧

newvue({
el:“#应用程序”,
数据:()=>({
投入:[{
用户名:“”
}],
}),
方法:{
加(){
this.inputs.push({用户名:'});
}
}
});

添加
更简单地说:

使用索引。为我工作

享受吧

newvue({
el:“#应用程序”,
数据:()=>({
投入:[{
用户名:“”
}],
}),
方法:{
加(){
this.inputs.push({用户名:'});
}
}
});

添加

不知道我可以使用
this.inputs.length
这样,以为它只会输出当前值,但我猜对象在该行运行之前被推入数组?非常好!谢天谢地,我不知道我可以使用
this.inputs.length
之类的,虽然它只会输出当前值,但我猜在该行运行之前对象会被推入数组中?非常好!谢谢