【发布时间】:2021-03-28 23:22:10
【问题描述】:
ScrollIntoView 返回未定义
导航到该页面时,我试图滚动到元素中:
scrollTo(itemId) {
this.$refs['item' + itemId].scrollIntoView({
behavior: 'smooth',
block: 'center',
inline: 'nearest',
})
},
关键是它在挂载的钩子内部被调用。 ref 已定义 itemId 也已定义,它是来自 vue 路由器的查询参数,但它仍然抛出 挂载钩子中的错误(Promise/async):“TypeError: Cannot read property 'scrollIntoView' of undefined”
出了什么问题,如何解决?
【问题讨论】:
-
如何调用
scrollTo以及如何引用元素? -
@BoussadjraBrahim 我在 div 中设置: :ref="'item' + item.itemId" 它在生成的循环内。当我调用 scrollTo 时,在挂载的钩子中我设置了参数 this.$route.query.itemId
-
@BoussadjraBrahim 看起来原因是这样的: this.$refs['item' + itemId] 返回 undefined 但是当我 console.log this.$refs 它返回对象 item889 存在。 889 也作为 itemId 传递给方法,为什么它会抛出 undefined?
标签: vue.js vuejs2 vue-component vue-router