中等Vue
0 1

Vue中怎么获取 DOM节点?

【概念释义】

获取 DOM:ref(推荐)、$el(根 DOM)、querySelector(不推荐破坏封装)。操作 DOM 应尽量少,用数据驱动;集成 jQuery/图表库时在 mounted + ref。

【基础使用】

vue
<input ref="input" />

mounted() {
  this.$refs.input.focus();
  console.log(this.$el);
}

【版本差异】

Vue 3 ref 在 script 为 ref 对象;$el 组件为根节点(Fragment 需注意)。

【拓展延伸】

登录查看完整 689

回答讨论

暂无讨论,登录后可抢先发言

本题库更多题目(50)