当前位置: 首页 > news >正文

第二十六章 Vue之在当前组件范围内获取dom元素和组件实例

目录

一、概述 

二、获取dom

2.1. 具体步骤

2.2. 完整代码 

2.2.1. main.js

2.2.2. App.vue

2.3. BaseChart.vue

三、获取组件实例

3.1. 具体步骤

3.2. 完整代码

3.2.1. main.js

3.2.2. App.vue

3.2.3. BaseForm.vue

3.3. 运行效果


一、概述 

我们过去在想要获取一个dom元素的时候,一般会使用到document.querySelector('class样式')这种全页面范围的查找方式。如果在页面比较复杂(如有多个组件且可能存在相同样式)的情况下,通过这种方式就获取就难以定位一个dom元素。因此Vue为我们提供了ref和$refs。

 

querySelector 查找范围 → 整个页面 

作用:利用ref和$refs可以用于获取dom元素组件实例。

特点:查找范围 → 当前组件内 (更精确稳定)

二、获取dom

2.1. 具体步骤

1. 目标标签 – 添加 ref 属性

2. 恰当时机, 通过 this.$refs.xxx, 在当前组件内查找获取目标标签

2.2. 完整代码 

2.2.1. main.js

import Vue from 'vue'
import App from './App.vue'Vue.config.productionTip = falsenew Vue({render: h => h(App),
}).$mount('#app')

2.2.2. App.vue

<template><div class="app"><div class="base-chart-box">这是一个捣乱的盒子</div><BaseChart></BaseChart></div>
</template><script>
import BaseChart from './components/BaseChart.vue'
export default {components:{BaseChart}
}
</script><style>
.base-chart-box {width: 300px;height: 200px;
}
</style>

2.3. BaseChart.vue

<template><div class="base-chart-box" ref="baseChartBox">子组件</div>
</template><script>
import * as echarts from 'echarts'export default {mounted() {// 基于准备好的dom,初始化echarts实例// document.querySelector 会查找项目中所有的元素// $refs只会在当前组件查找盒子// var myChart = echarts.init(document.querySelector('.base-chart-box'))var myChart = echarts.init(this.$refs.baseChartBox)// 绘制图表myChart.setOption({title: {text: 'ECharts 入门示例',},tooltip: {},xAxis: {data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子'],},yAxis: {},series: [{name: '销量',type: 'bar',data: [5, 20, 36, 10, 10, 20],},],})},
}
</script><style scoped>
.base-chart-box {width: 400px;height: 300px;border: 3px solid #000;border-radius: 6px;
}
</style>

三、获取组件实例

3.1. 具体步骤

作用:利用 ref 和 $refs 可以用于 获取 dom 元素, 组件实例

② 获取组件:

1. 目标组件 – 添加 ref 属性

2. 恰当时机, 通过 this.$refs.xxx, 获取目标组件,

就可以调用组件对象里面的方法

3.2. 完整代码

3.2.1. main.js

import Vue from 'vue'
import App from './App.vue'Vue.config.productionTip = falsenew Vue({render: h => h(App),
}).$mount('#app')

3.2.2. App.vue

<template><div class="app"><BaseForm ref="baseForm"></BaseForm><button @click="handleGet">获取数据</button><button @click="handleReset">重置数据</button></div>
</template><script>
import BaseForm from './components/BaseForm.vue'
export default {data () {return {}},methods: {handleGet () {console.log(this.$refs.baseForm.getValues())},handleReset () {this.$refs.baseForm.resetValues()}},components:{BaseForm}
}
</script><style></style>

3.2.3. BaseForm.vue

<template><form action="">账号:<input type="text" v-model="account"/><br><br>密码:<input type="text" v-model="password"/><br><br></form>
</template><script>
export default {data () {return {account: '',password: ''}},methods: {// 1. 收集表单数据,返回一个对象getValues () {return {account: this.account,password: this.password}},// 2. 重置表单resetValues () {this.account = '',this.password = ''}}
}
</script><style scoped>
.base-chart-box {width: 400px;height: 300px;border: 3px solid #000;border-radius: 6px;
}
</style>

3.3. 运行效果

父组件App.vue通过ref和$refs直接调用子组件BaseForm的方法获取和重置数据。

http://www.lryc.cn/news/475393.html

相关文章:

  • Markdown 区块
  • ctf文件上传题小总结与记录
  • 什么是QAM
  • GraphQL 与 Elasticsearch 相遇:使用 Hasura DDN 构建可扩展、支持 AI 的应用程序
  • 面试题整理 3
  • 数据结构(Java)—— 认识泛型
  • 处理后的视频如何加上音频信息?
  • 02LangChain 实战课——安装入门
  • Python函数中关键字参数、位置参数、默认参数有何不同
  • PNG 格式和 JPG 格式都什么时候用
  • Qt 练习做一个登录界面
  • 计算机视觉实验一:图像基础处理
  • 【WebApi】C# webapi 后端接收部分属性
  • Java 使用 Redis
  • 【ONE·Linux || 高级IO(二)】
  • 将 IBM WatsonX 数据与 Milvus 结合使用,构建用于知识检索的智能 Slack 机器人
  • 2024 网鼎杯 CTF --- Crypto wp
  • 深度学习基础知识-损失函数
  • 《逆向记录》
  • chatgpt3.5权重参数有多少MB;llama7B权重参数有多少MB
  • ST IoT Wireless 物联网与无线技术 研讨会
  • PHP实现雪花算法生成唯一ID
  • APP的设置页面,应该怎样尽可能减少用户的输入操作呢
  • Node.js:内置模块
  • 3. keil + vscode 进行stm32协同开发
  • React 组件生命周期与 Hooks 简明指南
  • 【springcloud】gateway网关的作用
  • 「C/C++」C++11 之<thread>多线程编程
  • HTML前端页面设计静态网站-仿百度
  • 百度SEO是否还有用?福州百度SEO专家林汉文为你深度解析