【问题标题】:The left-hand side of an assignment expression must be a variable or a property access in angular2赋值表达式的左边必须是 angular2 中的变量或属性访问
【发布时间】:2018-06-12 13:13:52
【问题描述】:

我遇到一个错误说

 The left-hand side of an assignment expression must be a variable or a property access.

使用这组代码,我遇到了这个错误,请任何人解决这个问题。

 this.isDeviceType = navigator.userAgent.match(/iPad/i)!= null ? ===  "iPad" ?
         "iOS" : navigator.userAgent.match(/iPhone/i)!= null ?=== "iPhone" ? 
         "iOS" : navigator.userAgent.match(/Android/i)!= null ? === 
 "Android" ? 
         "Android" : navigator.userAgent.match(/BlackBerry/i)!= null ?=== "BlackBerry" ? 
         "BlackBerry" : "Browser" 

【问题讨论】:

    标签: angular


    【解决方案1】:

    您正在将数组与字符串进行比较。 您可以使用test 代替match

    var isDeviceType = (/iPad/i).test(navigator.userAgent) ?
      "iOS" : (/iPhone/i).test(navigator.userAgent) ?
      "iOS" : (/Android/i).test(navigator.userAgent) ?
      "Android" : (/BlackBerry/i).test(navigator.userAgent) ?
      "BlackBerry" : "Browser";
    
    console.log(isDeviceType)

    【讨论】:

    • 错误 TS2365:运算符 '===' 不能应用于类型 'RegExpMatchArray' 和 'string'。
    • 改用'=='
    【解决方案2】:

    String.match 提供一个实际匹配的数组,如果没有找到,则返回 null。

    所以使用以下模式:

    navigator.userAgent.match(/.../i) != null ? "..." : ...
    

    【讨论】:

    • 感谢您的回复,我应该在任何地方都包含 != null 吗?
    • 是的,因为每个匹配项都应该被测试为不为空。
    • @callback 的解决方案还是更好。
    猜你喜欢
    • 2018-05-04
    • 2011-10-31
    • 1970-01-01
    • 2015-07-07
    • 2017-05-28
    • 2016-05-09
    • 2018-08-10
    • 2012-06-29
    • 1970-01-01
    相关资源
    最近更新 更多