js,在字符串中,查找某个字符的位置

// 获取查找某个字符的位置

方法:

findPosition(str, queryStr, pon) {
  var x = str.indexOf(queryStr)
  for (var i = 0; i < pon; i++) {
    x = str.indexOf(queryStr, x + 1)
  }
  return x
}

调用:
第二次出现 / 的位置

// '/home/pb-management'
this.findPosition(path, '/', 1)

应用场景
刷新页面时,导航菜单高亮显示

created() {
  const _self = this
  const route = _self.$route
  _self.$nextTick(() => {
    let path = route.path
    let findPosition = _self.findPosition(path, '/', 1)
    let activePath = path.substring(findPosition + 1)
    if (activePath) {
      _self.defaultActive = activePath
    }
  })
},

如果对您有所帮助,给个一键三连吧~~~Thanks♪(・ω・)ノ (✪ω✪) ᕦ(・ㅂ・)ᕤ!!!

你可能感兴趣的:(vue,JavaScript,js)