[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>0. 整体代码:
【父组件】:
<template> <div> <span style="color: red">------------------------------------
-------</span><br> <span style="color: red">这是父组件页面</span> <button @click=
"clickParentMethod">点击</button> <SubComponent ref="SubComponent"
@callParentMethod="callParentMethod" trans-data="传递的参数"/> </div> </template> <
script> import SubComponent from "@/components/SubComponent"; export default {
name: "SideOne", components: { SubComponent }, methods: { clickParentMethod () {
console.log('clickParentMethod') this.$refs.SubComponent.callSubMethod() },
callParentMethod (val) { console.log('callParentMethod: ' + val) } } } </script>
【子组件】:
<template> <div> <span>-------------------------------------------</span><br> <
span>这是子组件页面</span> <button @click="clickSubMethod">点击</button> </div> </
template> <script> export default { name: "SubComponent", props: { transData: {
type: String } }, methods: { clickSubMethod() { console.log('clickSubMethod')
this.$emit('callParentMethod','回调的参数') }, callSubMethod() { console.log(
'callSubMethod: ' + this.transData) } } } </script>
<>1. 要点:
<SubComponent ref="SubComponent" @callParentMethod="callParentMethod" trans-
data="传递的参数"/>
【整体效果】:
<> 1.1. 父组件调用子组件方法:
* 子组件标签添加ref属性,值为子组件,如ref=“SubComponent”
* 父组件this.$refs.SubComponent.callSubMethod()调用子组件方法
<> 1.2. 子组件回调父组件方法:
* 子组件标签绑定回调方法,如@callParentMethod=“callParentMethod”
* 子组件 this.$emit(‘callParentMethod’,‘回调的参数’)回调父组件方法
* 子路由回调父路由同样适用,在< router-view > 标签内绑定回调方法即可
<> 1.3. 父组件传递参数给子组件:
* 子组件标签绑定参数变量和传递的值,如trans-data=“传递的参数”,也可以使用:transData='param’
形式,param要在data中定义
* 子组件使用props来接收