window.location.search用正则获取不到地址栏指定参数

83 阅读1分钟
   getQueryStringhash(key) {
      // window.location.search只能获取?之后#之前的数据。如果?之后没有#则值为空。
      let after = window.location.search
      console.log(after.indexOf('?'))
      if (after && after.indexOf('?') === -1) return null // 如果url中没有传参直接返回空
      // key存在先通过search取值如果取不到就通过hash来取
      after = after.substr(1) || window.location.hash.split('?')[1]
      if (after) {
        const reg = new RegExp('(^|&)' + key + '=([^&]*)(&|$)')
        const r = after.match(reg)
        console.log(r, '...')
        if (r != null) {
          return decodeURIComponent(r[2])
        } else {
          return null
        }
      }
    },
this.getQueryStringhash('fileCode')