vue父组件调用子组件方法(运用)
父组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | < template > < div id="data-view"> < dv-full-screen-container > < div class="mc-top"> < water-level-chart :newData="data" ref="wlc"/> </ div > </ dv-full-screen-container > </ div > </ template > < script > import waterLevelChart from './waterLevelChart' export default { name: 'DataView', components: { waterLevelChart }, data () { return { data: [] } }, methods: { get () { this.$http({ method: 'get', url: '/test/test' }).then( (resp) => { this.data = resp.data this.$refs.wlc.get(resp.data) }, (error) => { console.log('error=' + JSON.stringify(error)) }).catch((exception) => { }) } } } </ script > |
子组件
<template> <div id="water-level-chart"> <div class="water-level-chart-title">FPY Summary {{newData.fpy}}%</div> <div class="chart-container"> <dv-water-level-pond :config="config"/> </div> </div> </template> <script> export default { props: ['newData'], data () { return { config: { data: [0], shape: 'round', waveHeight: 25, colors: ['rgb(133,165,255)'], waveNum: 2 } } }, methods: { get (newData) { const newConfig = { data: [newData.fpy], shape: 'round', waveHeight: 25, colors: ['rgb(133,165,255)'], waveNum: 2 } this.config = newConfig } } } </script> <style lang="less"> </style>
1、在子组件中:<div></div>是必须要存在的
2、在父组件中:首先要引入子组件 import waterLevelChart from './waterLevelChart';
3、 <water-level-chart ref="wlc"></water-level-chart>是在父组件中为子组件添加一个占位,ref="wlc"是子组件在父组件中的名字
4、父组件中 components: { 是声明子组件在父组件中的名字
5、在父组件的方法中调用子组件的方法,很重要 this.$refs.wlc.get("可传入值子组件接收");
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~