1、vue如何获取真实的DOM元素?
--可以通过 id 获取--
--还可以通过 ref定义值 $refs 获取--
<template> <div> <h1 ref="username" id="a">获取原生的DOM</h1> </div> </template> <script> export default { //钩子函数 mounted 加载完DOM之后才能获取 mounted() { console.log(this.$refs.username); //<h1>获取原生的DOM</h1> console.log(document.getElementById("a")); // <h1id="a">获取原生的DOM</h1id=> }, }; </script> <style> </style>
2、获取子组件的对象方法
--创建一个子组件,写一个方法--
--引入到父组件,注册,使用,添加ref定义值,通过$refs获取子组件的方法
1、-----------子组件------------------ <template> <div><p>我是一个组件</p></div> </template> <script> export default { methods: { fn()