zhangbo1_wd

解决图文详情web页面,偶现crash问题【Init error, The Webviewcontroller must be associated with a Web component】

@@ -12,6 +12,8 @@ const TAG = 'WdWebLocalComponent'; @@ -12,6 +12,8 @@ const TAG = 'WdWebLocalComponent';
12 @Component 12 @Component
13 export struct WdWebLocalComponent { 13 export struct WdWebLocalComponent {
14 webviewControl: BridgeWebViewControl = new BridgeWebViewControl() 14 webviewControl: BridgeWebViewControl = new BridgeWebViewControl()
  15 + onWebPrepared: () => void = () => {
  16 + }
15 @Prop backVisibility: boolean = false 17 @Prop backVisibility: boolean = false
16 @Prop webResource: Resource = {} as Resource 18 @Prop webResource: Resource = {} as Resource
17 @State webHeight: string | number = '100%' 19 @State webHeight: string | number = '100%'
@@ -106,6 +108,7 @@ export struct WdWebLocalComponent { @@ -106,6 +108,7 @@ export struct WdWebLocalComponent {
106 } 108 }
107 onPageEnd: (url?: string) => void = () => { 109 onPageEnd: (url?: string) => void = () => {
108 Logger.debug(TAG, 'onPageEnd'); 110 Logger.debug(TAG, 'onPageEnd');
  111 + this.onWebPrepared()
109 } 112 }
110 onLoadIntercept: (url?: string) => boolean = () => { 113 onLoadIntercept: (url?: string) => boolean = () => {
111 Logger.debug(TAG, 'onLoadIntercept return false'); 114 Logger.debug(TAG, 'onLoadIntercept return false');
@@ -17,6 +17,9 @@ export struct ImageAndTextWebComponent { @@ -17,6 +17,9 @@ export struct ImageAndTextWebComponent {
17 @State reload: number = 0; 17 @State reload: number = 0;
18 @Prop @Watch('onDetailDataUpdated') contentDetailData: ContentDetailDTO [] = [] as ContentDetailDTO [] 18 @Prop @Watch('onDetailDataUpdated') contentDetailData: ContentDetailDTO [] = [] as ContentDetailDTO []
19 webviewControl: BridgeWebViewControl = new BridgeWebViewControl() 19 webviewControl: BridgeWebViewControl = new BridgeWebViewControl()
  20 + private h5ReceiveAppData: H5ReceiveDetailBean = { dataSource: '2' } as H5ReceiveDetailBean
  21 + private webPrepared = false;
  22 + private dataPrepared = false;
20 23
21 onDetailDataUpdated() { 24 onDetailDataUpdated() {
22 if (this.action) { 25 if (this.action) {
@@ -50,7 +53,6 @@ export struct ImageAndTextWebComponent { @@ -50,7 +53,6 @@ export struct ImageAndTextWebComponent {
50 53
51 } 54 }
52 55
53 - let h5ReceiveAppData: H5ReceiveDetailBean = { dataSource: '2' } as H5ReceiveDetailBean  
54 // TODO 对接user信息、登录情况 56 // TODO 对接user信息、登录情况
55 let h5ReceiveDataExtraBean: H5ReceiveDataExtraBean = { creatorId: '', isLogin: '0' } as H5ReceiveDataExtraBean 57 let h5ReceiveDataExtraBean: H5ReceiveDataExtraBean = { creatorId: '', isLogin: '0' } as H5ReceiveDataExtraBean
56 let h5ReceiveDataJsonBean: H5ReceiveDataJsonBean = { 58 let h5ReceiveDataJsonBean: H5ReceiveDataJsonBean = {
@@ -67,12 +69,10 @@ export struct ImageAndTextWebComponent { @@ -67,12 +69,10 @@ export struct ImageAndTextWebComponent {
67 response.code = 200 69 response.code = 200
68 response.success = true 70 response.success = true
69 h5ReceiveDataJsonBean.responseMap = response 71 h5ReceiveDataJsonBean.responseMap = response
70 - h5ReceiveAppData.dataJson = h5ReceiveDataJsonBean  
71 - h5ReceiveAppData.dataExt = h5ReceiveDataExtraBean  
72 - // TODO 暂延时1s,考虑业务流程再优化  
73 - setTimeout(() => {  
74 - this.sendContentData2H5(h5ReceiveAppData);  
75 - }, 2000) 72 + this.h5ReceiveAppData.dataJson = h5ReceiveDataJsonBean
  73 + this.h5ReceiveAppData.dataExt = h5ReceiveDataExtraBean
  74 + this.dataPrepared = true
  75 + this.trySendData2H5()
76 76
77 } 77 }
78 78
@@ -84,10 +84,24 @@ export struct ImageAndTextWebComponent { @@ -84,10 +84,24 @@ export struct ImageAndTextWebComponent {
84 webviewControl: this.webviewControl, 84 webviewControl: this.webviewControl,
85 webResource: $rawfile('apph5/index.html'), 85 webResource: $rawfile('apph5/index.html'),
86 backVisibility: false, 86 backVisibility: false,
  87 + onWebPrepared: this.onWebPrepared.bind(this)
87 }) 88 })
88 } 89 }
89 } 90 }
90 91
  92 + private trySendData2H5() {
  93 + if (!this.webPrepared || !this.dataPrepared) {
  94 + return
  95 + }
  96 + // 数据、web组件,都准备好了,开始塞详情数据
  97 + this.sendContentData2H5(this.h5ReceiveAppData)
  98 + }
  99 +
  100 + private onWebPrepared() {
  101 + this.webPrepared = true
  102 + this.trySendData2H5()
  103 + }
  104 +
91 private sendContentData2H5(h5ReceiveAppData: H5ReceiveDetailBean) { 105 private sendContentData2H5(h5ReceiveAppData: H5ReceiveDetailBean) {
92 Logger.debug('ImageAndTextWebComponent', 'jsCall_receiveAppData'); 106 Logger.debug('ImageAndTextWebComponent', 'jsCall_receiveAppData');
93 this.webviewControl.callHandle(NativeCallH5Type.jsCall_receiveAppData, 107 this.webviewControl.callHandle(NativeCallH5Type.jsCall_receiveAppData,