【问题标题】:Vue JS if/else statment inside computed property计算属性中的 Vue JS if/else 语句
【发布时间】:2019-03-01 12:14:54
【问题描述】:

我正在尝试在 Vue JS 的计算属性中执行 if / else 语句以进行搜​​索,这就是我所拥有的,但它不起作用,我该如何调整它以使其工作?

computed: {
    filteredProperties: function(){
      return this.properties.filter((property) => {
        return property.address.match(this.searchAddress) &&

        if (this.searchType.length > 1) {
          this.searchType.some(function(val){
            return property.type.match(val)
          }) &&
        } else {
          property.type.match(this.searchType) &&
        }

        property.bedrooms.match(this.searchBedrooms) &&
        property.county.match(this.searchCounty)
      });
    }
  }

【问题讨论】:

  • it's not working 在哪种情况下不起作用?
  • 像您使用的&& 是无效的... && if (condition) {} && 是无效的。 }) && } else { - 绝对无效。
  • @tymeJV 我怎样才能使它有效?
  • 您可以使用三元组,也可以将if 逻辑的结果分配给一个变量,然后检查该变量。

标签: javascript vue.js vuejs2 nuxt.js


【解决方案1】:

您的语法错误,不能在表达式中间使用 if 语句。这会起作用:

computed: {
  filteredProperties: function(){
    return this.properties.filter((property) => {

    let searchTypeMatch = this.searchType.length > 1
      ? this.searchType.some(function(val){
        return property.type.match(val)
      })
      : property.type.match(this.searchType)

    return property.address.match(this.searchAddress) &&
      searchTypeMatch &&
      property.bedrooms.match(this.searchBedrooms) &&
      property.county.match(this.searchCounty)
    });
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-17
    • 1970-01-01
    • 1970-01-01
    • 2018-11-04
    • 2017-06-30
    • 1970-01-01
    • 2016-06-28
    相关资源
    最近更新 更多