解决vue路由与锚点冲突

时间:2022-09-10 16:15:08

传统的锚点定位会与vue-router中的路由设置存在冲突,解决方法自定义锚点跳转:

html:

<ul>
<li><a href="" @click.prevent="custormAnchor('firstAnchor')">公共报文头</a></li>
</ul>

js:

methods: {
custormAnchor(anchorName) {
// 找到锚点
let anchorElement = document.getElementById(anchorName);
// 如果对应id的锚点存在,就跳转到锚点
if(anchorElement) { anchorElement.scrollIntoView(); }
}
}