钉钉小程序(企业内部应用)开发--钉钉小程序web-view嵌套H5与小程序之间的通信(H5跳转钉钉小程序小程序postMessage)

钉钉小程序代码:嵌套H5 pages/login/index.axml


H5向小程序发送信息:
H5代码:
通过以下代码我一直报错dd没有被定义

if (navigator.userAgent.toLowerCase().indexOf('dingtalk') > -1) {
	
		document.writeln('

钉钉小程序(企业内部应用)开发--钉钉小程序web-view嵌套H5与小程序之间的通信(H5跳转钉钉小程序小程序postMessage)_第1张图片
在h5页面中引入以上代码成功使用dd

H5点击按钮向H5发送信息
H5页面代码:
 


		向小程序发送信息
		

test() {
				// 判断当前是否处于钉钉环境
				if (navigator.userAgent.toLowerCase().indexOf("dingtalk") > -1) {
					// 下载dd资源 这种引入钉钉也报错
					if (!window.dd) {
						// console.log("追加dd");
						//     let script = document.createElement("script");
						//     script.src = "https://appx/web-view.min.js";
						//     script.async = true;
						//     document.head.appendChild(script);
						// console.log("追加dd1");
					}
					console.log("通信", window);
					// H5向钉钉小程序通信
					window.dd && window.dd.postMessage({
						noticeDetail: JSON.stringify(this.data),
					});
				}
			},

小程序端接受信息 代码:pages/login/index.js

  test(e){
     console.log("获取数据",e.detail.noticeDetail);

     dd.setStorageSync({
      key: 'noticeDetail',
      data: e.detail.noticeDetail
    });
//接受成功后做其他操作
    dd.navigateTo({
   
      url: "/pages/newsDetail/index"
  })
  }

你可能感兴趣的:(钉钉,小程序,前端)