HarmonyOS 关于Provide数据传递问题咨询

Consulting description:特殊类型的属性如何不通过props传递,封装地图组件的时候,在父容器组件中定义:

@Provide map?: map.MapComponentController = undefined 
aboutToAppear(): void { 
  this.mapCallback = async (err, mapController) => { 
    if (!err) { 
      this.map = mapController; 
    } 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

然后在子组件中使用@Consume map?: map.MapComponentController接收,运行实例的时候app会直接闪退,有没有更好的解决方式

Scenario:封装地图控件,希望最终使用方式如下:

Map({...}){ 
  Marker({...}) 
  Circle({...}) 
}
  • 1.
  • 2.
  • 3.
  • 4.

由于在marker里面需要调用map.addMarker(options),不希望所有的map都通过props传递下去,有什么更好的解决方案。

HarmonyOS
2024-08-09 15:51:25
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

问题解决方案如下:

// Demo 
import { Map } from './map' 
@Entry 
@Component 
export struct Demo { 
  // 地图初始化参数 
  build() { 
    Column(){ 
      Map({ 
        options: { 
          position: { 
            target: { 
              latitude: 31.985019, 
              longitude: 118.765998 
            }, 
            zoom: 14 
          }, 
          scaleControlsEnabled: true 
        } 
      }) 
    } 
  } 
}
// Map 
import { map, mapCommon, MapComponent } from '@kit.MapKit'; 
import { AsyncCallback } from '@kit.BasicServicesKit'; 
import {MapMarker} from './Marker' 
@Entry 
@Component 
export struct Map { 
  // 地图初始化参数 
  options?: mapCommon.MapOptions; 
  // 地图初始化回调 
  callback?: AsyncCallback<map.MapComponentController>; 
  @Provide map?: map.MapComponentController = undefined 
  @BuilderParam closer: () => void; 
  build() { 
    Column() { 
      Column() { 
        MapMarker({ 
          MarkerOptions: { 
            position: { 
              latitude: 31.985019, 
              longitude: 118.765998 
            }, 
            rotation: 0, 
            visible: true, 
            zIndex: 0, 
            alpha: 1, 
            anchorU: 0.5, 
            anchorV: 1, 
            clickable: true, 
            draggable: true, 
            flat: false 
          }, 
          MapOptions:this.options 
        }) 
      } 
    }.width('100%').height('100%').backgroundColor('#ff9008') 
  } 
}
// Maker 
import { map, mapCommon, MapComponent } from '@kit.MapKit'; 
import { AsyncCallback } from '@kit.BasicServicesKit'; 
@Component 
export struct MapMarker { 
  @Consume map?: map.MapComponentController; 
  private mapCallback?: AsyncCallback<map.MapComponentController>; 
  MapOptions?: mapCommon.MapOptions; 
  // Marker初始化参数 
  MarkerOptions?: mapCommon.MarkerOptions; 
  callback?: AsyncCallback<map.MapComponentController>; 
  async aboutToAppear() { 
    this.mapCallback = async (err, mapController) => { 
      if (!err) { 
        this.map = mapController; 
        console.log('this.map'+JSON.stringify(this.map)) 
        this.callback?.(err, mapController) 
      } 
    } 
    build() { 
      MapComponent({ mapOptions: this.MapOptions, mapCallback: this.mapCallback }).width('100%').height('100%') 
    } 
  }
  • 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.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
分享
微博
QQ
微信
回复
2024-08-09 18:56:04


相关问题
HarmonyOS 数据传递问题
1078浏览 • 1回复 待解决
HarmonyOS axios上传文件数据传递问题
546浏览 • 1回复 待解决
关于数据缓存问题咨询
978浏览 • 1回复 待解决
HarmonyOS worker间数据传递
633浏览 • 1回复 待解决
HarmonyOS router.getParams()数据传递
1182浏览 • 1回复 待解决
HarmonyOS taskpool数据传问题
1146浏览 • 1回复 待解决
关于http请求数据发送异常问题咨询
1016浏览 • 1回复 待解决
HarmonyOS 关于vp的问题咨询
752浏览 • 1回复 待解决
多hap调用及数据传递,有人知道吗?
1635浏览 • 1回复 待解决
关于系统定位缓存问题咨询
1413浏览 • 1回复 待解决
HarmonyOS 关于仓颉的咨询问题
633浏览 • 1回复 待解决
聊一聊关于流转问题咨询
7400浏览 • 1回复 待解决
HarmonyOS 关于react-native-fs问题咨询
934浏览 • 1回复 待解决
HarmonyOS 关于router.back问题咨询
1028浏览 • 1回复 待解决
关于JS http请求参数的传递问题
8205浏览 • 2回复 待解决
恭喜您,今日已阅读两篇内容,特奖励+2声望, 快来领取吧。