1. 程式人生 > >路徑傳值字串型別,比較總是返回false遇到的坑

路徑傳值字串型別,比較總是返回false遇到的坑

我的需求是在A頁面傳值type跳轉到B頁面,B頁面接收,然後處理邏輯

A頁面:

<navigator class="search-box" open-type="navigate" url="../search/search?type='PinDuoDuo'" hover-class="none">  </navigator>

B頁面接收值:
 onLoad: function(options) {
    let str = options.type;
    console.log('str的型別:' + typeof (str))   //str的型別:string
    console.log('str的值是:' + str)            //str的值是:'PinDuoDuo'
    console.log(str=="PinDuoDuo")              //false
    console.log(str=='PinDuoDuo')              //false
  },

我就很納悶,為什麼會是false,明明型別都是string。後來一朋友提醒,說比較的時候型別就變了。

console.log(str =="'PinDuoDuo'")      //返回true

弄明白了這個,後面的就好說,直接賦值還是不行 ,一定要賦值字串。

//剛開始賦值如下,達不到我要的效果
if (str == "'PinDuoDuo'") {
    this.setData({
        currentTab: str,
    })
}

//最後改成下面的,就可以了
if (str == "'PinDuoDuo'") {
    this.setData({
        currentTab: 'PinDuoDuo',
    })
}

為什麼第一種方式賦值沒用,因為我頁面上還做了一次判斷比較,所有必須賦值字串才可以