在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/網(wǎng)絡(luò)安全  HTML/ vue spa項目中 window.location.hash 異常

vue spa項目中 window.location.hash 異常

為什么window.location.hash = this.$route.hash不起作用呢

mounted() {
  this.$route.hash && (window.location.hash = this.$route.hash)
  // setTimeout(() => {
  //   console.log(this.$route.hash)
  //   this.$route.hash && (window.location.hash = this.$route.hash)
  // }, 2000)
},
watch: {
  '$route.hash'() {
    setTimeout(() => {
      console.log(this.$route.hash)
      this.$route.hash && (window.location.hash = this.$route.hash)
    }, 5000)
    // this.$route.hash && (window.location.hash = this.$route.hash)
  }
},

下面兩種情況都是會起作用的
1.hash寫死

watch: {
  '$route.hash'() {
    setTimeout(() => {
      console.log(this.$route.hash)
      this.$route.hash && (window.location.hash = this.$route.hash)
    }, 5000)
    // this.$route.hash && (window.location.hash = '#test')
  }
},

2.寫在事件中

<template>
    <a @click="pushState(url)"></a>
</template>

methods: {
  pushState(url) {
    window.location.hash = `#${url.split('#')[1]}`
  }
}
回答
編輯回答
抱緊我

你watch了hash,然后又改變hash,不會進(jìn)入死循環(huán)嗎

2017年7月2日 02:15
編輯回答
不二心

你先把this.$route.hash這個打印出來,應(yīng)該是這個沒值

2018年9月21日 03:42
編輯回答
耍太極

異步里面有this?

2017年10月31日 10:59
編輯回答
神經(jīng)質(zhì)

我通過另一種方式實現(xiàn)了錨點的跳轉(zhuǎn)

goAnchor(selector) {
  const anchor = this.$el.querySelector(selector)
  document.documentElement.scrollTop = anchor.offsetTop
}

參考vue2.0中怎么做錨點定位

2017年4月13日 10:59