Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.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
Methods 使用computed or方法获取v-for内的索引_Methods_Vue.js_Computed Properties_V For - Fatal编程技术网

Methods 使用computed or方法获取v-for内的索引

Methods 使用computed or方法获取v-for内的索引,methods,vue.js,computed-properties,v-for,Methods,Vue.js,Computed Properties,V For,我想知道在vue中是否可以直接在v-for-in中获取对象数组的索引,并将该值传递给计算属性或方法(类似于此处),甚至传递给计算属性 <div v-for="(object, index) in objects(index)"></div> methods: { objects(index){ const categoryId = Object.keys(this.data); return this.data[categoryId[index]

我想知道在vue中是否可以直接在v-for-in中获取对象数组的索引,并将该值传递给计算属性或方法(类似于此处),甚至传递给计算属性

<div v-for="(object, index) in objects(index)"></div> 

methods: {
   objects(index){
    const categoryId = Object.keys(this.data);
    return this.data[categoryId[index]].extras;
   } 
}

方法:{
对象(索引){
const categoryId=Object.keys(this.data);
返回此.data[categoryId[index]].extras;
} 
}

我需要索引,因为它更方便我根据定义的键返回正确的值,有什么方法可以实现这一点吗

使用计算出的值转换数据,并在该值上循环。我不确定你的
this.data
看起来像什么,但类似的东西应该可以工作(调整它以满足你的需要):


计算:{
计算对象(){
返回Object.keys(this.data).map(categoryId=>this.data[categoryId].extras)
} 
}

使用计算值转换数据,并在其上循环。我不确定你的
this.data
看起来像什么,但类似的东西应该可以工作(调整它以满足你的需要):


计算:{
计算对象(){
返回Object.keys(this.data).map(categoryId=>this.data[categoryId].extras)
} 
}

您可以对由
v-for
指令创建的每个元素绑定一个方法调用,因此,例如,每当用户单击
  • 元素时,它都会获取该单击项的索引:

    newvue({
    el:“#应用程序”,
    数据:{
    clickedIndex:null,
    工作日:[“周一”、“周二”、“周三”、“周四”、“周五”]
    },
    方法:{
    handleClick(一){
    单击此项。单击索引=i;
    }
    }
    })
    Vue.config.productionTip=false;
    Vue.config.devtools=false
    
    
    
    • {{day}
    索引已单击{clickedIndex}


    您可以对由
    v-for
    指令创建的每个元素绑定一个方法调用,因此,例如,每当用户单击
  • 元素时,它都会获取该单击项的索引:

    newvue({
    el:“#应用程序”,
    数据:{
    clickedIndex:null,
    工作日:[“周一”、“周二”、“周三”、“周四”、“周五”]
    },
    方法:{
    handleClick(一){
    单击此项。单击索引=i;
    }
    }
    })
    Vue.config.productionTip=false;
    Vue.config.devtools=false
    
    
    
    • {{day}
    索引已单击{clickedIndex}


    使用方法而不是计算方法。否。计算属性不能具有参数SUSES方法而不是computed。否。计算属性不能有参数我知道我没有很好地解释我的用例,我不需要实现点击不同元素的方式,我只需要将每个元素的索引传递到一个计算属性或方法中,以返回对象数组中的正确值。如果我没有很好地解释我的用例,我不需要实现单击不同元素的方法,我只需要将每个元素的索引传递到计算属性或方法中,就可以在对象数组中返回正确的值
    <div v-for="object in computed_objects"></div> 
    
    computed: {
       computed_objects(){
        return Object.keys(this.data).map(categoryId => this.data[categoryId].extras)
       } 
    }