javascript - vue js 等于字符串
问题描述
为什么filter
不使用this.userId
但使用硬代码值"admin"
?我怎样才能解决这个问题?
computed: {
UidMessages: function() {
return this.Messages.filter(function(m) {
return m.to == this.userId;
})
}
},
它确实有效=>
computed: {
AMesseg: function() {
return this.Messages.filter(function(m) {
return m.to== "admin"
})
}
},
我认为这是因为字符串的比较
谢谢。
解决方案
this
到达undefined
这里是因为它不绑定到您的fn
. 用于arrow syntax
词法绑定this
到fn
它应该可以工作
computed: {
UidMessages: function() {
return this.Messages.filter(m => {
return m.to == this.userId
})
}
},
推荐阅读
- python - 如何在 html 中使用 {% if user.is_authenticated %} 来显示或删除内容
- java - 为什么 ChronoUnit.HOURS.between() 不工作?
- javascript - 我可以在 Vscode 编辑器中使用 Live Server 吗?
- java - 如何在 Spring Boot 中测试分页结果?
- flutter - 对流有什么不好的用途?
- xcode - 为什么角色的脸在 Xcode 中会自动转换为绿色?
- javascript - 如何在 django 模板中设置默认值
- reactjs - 为什么两条路由在具有不同路径链接的反应js中调用同一页面
- javascript - 如何将href自动设置为另一个div onclick函数href
- amazon-web-services - 基于自定义 Cloudwatch 警报的自动缩放规则