场景
从 page/1
跳到 page/2#a
时,页面没有定位到锚点处
方法①
在有锚点的页面添加代码:
methods: {
goAnchor(selector) {
// 最好加个定时器给页面缓冲时间
setTimeout(() => {
// 获取锚点元素
let anchor = this.$el.querySelector(selector)
anchor.scrollIntoView()
}, 500)
}
},
mounted() {
if (window.location.hash) {
this.goAnchor(window.location.hash)
}
}
方法②
NuxtLink不支持带hash的跳转,可改为a链接跳转