- 转自 透过代码观世界
a组件跳转到b组件的指定位置
a组件中写跳转
<div class="content">
<a href="/b组件路由#锚点的id或name值">gotoOther</a>
</div>
在b组件中写方法:
methods: {
goAnchor(selector) {
let anchor = this.$el.querySelector(selector)
document.documentElement.scrollTop = anchor.offsetTop
},
}
在b组件的mounted中添加:
mounted:function(){
this.$nextTick(()=> {
//当页面地址发生改变时调用方法
this.goAnchor(window.location.hash)
})
},