Vue.js - 将 prop 传递给 $.each() (在 mounted 中)
Vue.js - Pass prop into $.each() (in mounted)
我需要在安装组件时迭代对象文字。但是我很难在 .each()
函数中使用道具数据。在 each()
函数中,当我用 console.log
检查时,道具数据是 'undefined'。我的代码如下所示:
[..]
props: {
active_categories: String, // example: "0,1,2,4", might also be NULL
category_list: Array // example: [ 0:{ID:0, Status:0}, 1:{ID:1, Status:0} [..] ]
},
mounted: function(){
// iterate through the prop array 'category_list'
// if an ID of this array is included in the string 'active_categories',
// set 'Status=1'
$.each(this.category_list, function(key, value, active_categories) {
if(active_categories){
if(active_categories.includes(value.ID)){
value.Status = 1;
}
}
});
如何将道具值带入我的 .each()
函数?
将 this
分配给一个名为 self
的全局变量,然后在 $.each
回调中使用它,因为 this
在该回调中具有不同的上下文:
mounted: function(){
let self=this
$.each(this.category_list, function(key, value) {
if(self.active_categories){
if(self.active_categories.includes(value.ID)){
value.Status = 1;
}
}
});
我需要在安装组件时迭代对象文字。但是我很难在 .each()
函数中使用道具数据。在 each()
函数中,当我用 console.log
检查时,道具数据是 'undefined'。我的代码如下所示:
[..]
props: {
active_categories: String, // example: "0,1,2,4", might also be NULL
category_list: Array // example: [ 0:{ID:0, Status:0}, 1:{ID:1, Status:0} [..] ]
},
mounted: function(){
// iterate through the prop array 'category_list'
// if an ID of this array is included in the string 'active_categories',
// set 'Status=1'
$.each(this.category_list, function(key, value, active_categories) {
if(active_categories){
if(active_categories.includes(value.ID)){
value.Status = 1;
}
}
});
如何将道具值带入我的 .each()
函数?
将 this
分配给一个名为 self
的全局变量,然后在 $.each
回调中使用它,因为 this
在该回调中具有不同的上下文:
mounted: function(){
let self=this
$.each(this.category_list, function(key, value) {
if(self.active_categories){
if(self.active_categories.includes(value.ID)){
value.Status = 1;
}
}
});