当前位置:网站首页>微信小程序子组件向页面传值(父子组件间的通信)带源码
微信小程序子组件向页面传值(父子组件间的通信)带源码
2022-06-29 08:26:00 【swag_特约男演员】
很多同学可能知道怎么在页面调用子组件内部方法,但是组件里的值怎么传到页面中来呢?
快使用事件触发!
要点:
- 要在组件的随便一个函数里,没错,就是随便,可以是生命周期如
attached,也可以是点击函数bindtap,看业务需求,创建一个触发器triggerEvent - 在页面引用的组件要绑定这个事件,因为你创建了事件触发器,你绑定了这个事件才能触发啊!
怎么创建触发器?
微信官方文档
https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/events.html

在页面使用

**因为这个事件触发器是在attached**创建的所以组件进入节点树就触发了
实用案例(组件表单值传给页面)
页面原始状态
当组件输入框失去焦点就把值传到页面
调用组件内部方法(同样可以把组件内部的值传给页面)
- 首先要在页面拿到组件实例,所以在页面要给组件分配id
- 其次才可以获取组件内部的数据
看效果
拿到组件实例,打印出来,是不是就可以拿到组件内部数据呢?

调用组件内部方法
用组件实例直接.出来调用就可以了
源码
组件
<template>
<view>
<input type="text" bindblur="getVal" placeholder="请输入" />
</view>
</template>
<script> import {
createComponent } from '@mpxjs/core' createComponent({
data: {
communicate: '我就是子组件传过来的值', a: '用于查看', b: '用于例子看起来多一点', c: 'example' }, methods: {
// 在输入框失去焦点的时候创建事件触发器并把表单值传过去 getVal(e) {
// console.log(e.detail.value) this.triggerEvent( 'customevent', {
msg: e.detail.value }, {
bubbles: true } ) }, // 组件内部方法 _inner() {
console.log('调用了组件内部方法') } } }) </script>
页面
<template>
<view class="container">
<view class="result">{
{msg}}</view>
<son-component id="my" bind:customevent="onTap" />
<button bindtap="inner">调用组件内部方法</button>
</view>
</template>
<script> import {
createPage } from '@mpxjs/core' createPage({
data: {
msg: '我还没被改变' }, methods: {
onTap(e) {
console.log(e.detail) this.msg = e.detail.msg }, inner() {
const myComponent = this.selectComponent('#my') // console.log(myComponent) // this.msg = myComponent.__data__.c myComponent._inner() } } }) </script>
<style lang="stylus" scoped> .container width 100% height 300px box-sizing border-box overflow hidden .result line-height 50px height 50px font-size 16px font-weight 600 text-align center </style>
<script type="application/json"> {
"navigationBarTitleText": "组件通信", "usingComponents": {
"son-component": "../components/event" } } </script>
边栏推荐
- 启牛学堂让开的证券账户是真的安全靠谱吗?
- The final of the sixth season of 2022 perfect children's model Hefei division came to a successful conclusion
- MT-yolov6训练及测试
- 图片格式-webp
- mysql insert 时出现Deadlock死锁场景分析
- Network learning of pointnet
- Let you know today that the passing rate of the PMP Exam is 97%, is it believable
- 工作好多年,回忆人生--高中三年
- How to recite words in tables
- General multiplier design, verilog code
猜你喜欢

图片格式-webp

io流的总结

Transformer details

hostname -f与uname -n的返回值可能不同

Does the SQL server run with administrator privileges? Or run it as a normal user?

闭关修炼(二十一)Servlet生命周期、service方法源码分析、线程安全问题

Unity C# 网络学习(十二)——Protobuf生成协议

Mysql使用union all统计多张表组合总数,并分别统计各表数量

2022 spring summer collection koreano essential reshapes the vitality of fashion

The sixth season of 2022 perfect children's model Qingyuan competition area audition came to a successful conclusion
随机推荐
(III) encoder self attention mask
Is it safe for the top ten securities companies to open accounts? Is it reliable?
The sixth season of 2022 perfect children's model Qingyuan competition area audition came to a successful conclusion
在 RedisTemplate 中使用 scan
Intelligent hardware EVT DVT PVT mp
Measure the level of various chess playing activities through ELO mechanism
Differences between x86 and x64
Working for many years, recalling life -- three years in high school
今年的网络安全“体检”你做了吗?
Let you know today that the passing rate of the PMP Exam is 97%, is it believable
宏,函数和内联函数
Déclaration de la variable Typescript - - assertion de type
Cdga | what is the core of digital transformation in the transportation industry?
Product manager certification enrollment brochure (NPDP) in July 2022
机器人代码生成器之Robcogen使用教程
【最全】PS各个版本下载安装及小试牛刀教程(PhotoShop CS3 ~~ PhotoShop 2022)
Let's make a summary
io流的总结
Unity C# 网络学习(十二)——Protobuf生成协议
手写VirtualDOM