fixed bug of anchor that will error when add new AnchorLink async

This commit is contained in:
zhigang.li 2018-04-12 21:23:48 +08:00
parent a1fb479092
commit 4556cfa8a5
3 changed files with 24 additions and 6 deletions

View file

@ -40,6 +40,11 @@ export default {
goAnchor () {
this.parentAnchor.turnTo(this.href);
}
},
mounted () {
this.$nextTick(() => {
this.parentAnchor.init();
});
}
};
</script>

View file

@ -112,13 +112,16 @@ export default {
const links = findComponentsDownward(this, 'AnchorLink').map(link => {
return link.href;
});
const offsetArr = links.map(link => {
const id = link.split('#')[1];
const idArr = links.map(link => {
return link.split('#')[1];
});
let offsetArr = [];
idArr.forEach(id => {
const titleEle = document.getElementById(id);
return {
link: link,
if (titleEle) offsetArr.push({
link: `#${id}`,
offset: titleEle.offsetTop - this.scrollElement.offsetTop
};
});
});
this.titlesOffsetArr = offsetArr;
},