首页 > 解决方案 > 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"
     })
   }
},

我认为这是因为字符串的比较

谢谢。

标签: javascriptvue.js

解决方案


this到达undefined这里是因为它不绑定到您的fn. 用于arrow syntax 词法绑定thisfn它应该可以工作

 computed: {
    UidMessages: function() {
       return this.Messages.filter(m => {
         return m.to == this.userId
     })
   }
  },

推荐阅读