{"id":526,"date":"2025-12-05T19:37:02","date_gmt":"2025-12-05T11:37:02","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=526"},"modified":"2025-12-05T19:37:02","modified_gmt":"2025-12-05T11:37:02","slug":"uni-app%e4%b9%8bwebview%e9%80%9a%e4%bf%a1%e5%ae%9e%e7%8e%b0%e6%96%b9%e6%b3%95%e7%a4%ba%e4%be%8b","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/05\/uni-app%e4%b9%8bwebview%e9%80%9a%e4%bf%a1%e5%ae%9e%e7%8e%b0%e6%96%b9%e6%b3%95%e7%a4%ba%e4%be%8b\/","title":{"rendered":"uni-app\u4e4bwebview\u901a\u4fe1\u5b9e\u73b0\u65b9\u6cd5\u793a\u4f8b"},"content":{"rendered":"\n<p>\u4e0b\u9762\u4e3a\u4f60\u6574\u7406\u4e00\u4efd <strong>uni-app \u4e2d WebView \u9875\u9762\u4e0e\u539f\u751f\u9875\u9762\/\u7236\u9875\u9762\u901a\u4fe1\u7684\u5b9e\u73b0\u65b9\u6cd5\u793a\u4f8b\uff08\u6700\u65b0\u7248\u793a\u4f8b + Vue3 \u7ed3\u6784\uff09<\/strong>\uff0c\u5305\u542b <strong>3 \u79cd\u5e38\u7528\u901a\u4fe1\u65b9\u5f0f<\/strong>\uff0c\u4ee5\u53ca <strong>\u5b8c\u6574\u53ef\u590d\u5236\u4ee3\u7801<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 uni-app \u4e4b WebView \u901a\u4fe1\u5b9e\u73b0\u65b9\u6cd5\u793a\u4f8b\uff08\u8d85\u8be6\u7ec6\uff09<\/h1>\n\n\n\n<p>\u5728 uni-app \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 <code>&lt;web-view&gt;<\/code> \u52a0\u8f7d H5 \u9875\u9762\uff0c\u800c H5 \u4e0e uni-app \u901a\u4fe1\u4e3b\u8981\u6709\u4e09\u79cd\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 \u65b9\u6cd5 1\uff1a\u4f7f\u7528 <code>postMessage<\/code>\uff08\u63a8\u8350\uff09<\/h1>\n\n\n\n<p>\u8fd9\u662f <strong>\u5b98\u65b9\u63a8\u8350\u7684\u53cc\u5411\u901a\u4fe1\u65b9\u5f0f<\/strong>\uff0c\u517c\u5bb9\u6027\u6700\u4f73\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udccc \u4e00\u3001\u7236\u9875\u9762\uff08uni-app\uff09\u5411 WebView \u53d1\u9001\u6d88\u606f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u7236\u9875\u9762\u4ee3\u7801\uff1apages\/webview\/webview.vue<\/strong><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;template&gt;\n  &amp;lt;web-view :src=&quot;url&quot; @message=&quot;onMessage&quot;&gt;&amp;lt;\/web-view&gt;\n&amp;lt;\/template&gt;\n\n&amp;lt;script setup&gt;\nimport { ref } from &quot;vue&quot;;\n\nconst url = ref(&quot;https:\/\/example.com\/test.html&quot;);\n\n\/\/ \u63a5\u6536\u6765\u81ea H5 \u7684\u6d88\u606f\nfunction onMessage(e) {\n  console.log(&quot;\u6536\u5230 H5 \u6d88\u606f\uff1a&quot;, e.detail.data);\n}\n&amp;lt;\/script&gt;\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udccc \u4e8c\u3001H5 \u9875\u9762\uff08test.html\uff09\u5411 uni-app \u53d1\u9001\u6d88\u606f<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;script&gt;\nfunction sendMsgToApp() {\n  uni.postMessage({\n    data: {\n      msg: &quot;hello uni-app&quot;\n    }\n  });\n}\n&amp;lt;\/script&gt;\n\n&amp;lt;button onclick=&quot;sendMsgToApp()&quot;&gt;\u53d1\u9001\u6d88\u606f\u7ed9uni-app&amp;lt;\/button&gt;\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udccc \u4e09\u3001uni-app \u4e3b\u52a8\u53d1\u9001\u6d88\u606f\u7ed9 H5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u7236\u9875\u9762\u4f7f\u7528\uff1a<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst webview = this.$scope.$getAppWebview().children()&#x5B;0];\nwebview.postMessage({ cmd: &quot;changeColor&quot;, value: &quot;red&quot; });\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">H5 \u7aef\u76d1\u542c\uff1a<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nwindow.addEventListener(&quot;message&quot;, function (event) {\n  console.log(&quot;\u6765\u81ea uni-app \u7684\u6d88\u606f\uff1a&quot;, event.data);\n});\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 \u65b9\u6cd5 2\uff1a\u4f7f\u7528\u4e8b\u4ef6\u76d1\u542c\uff08plus.webview\uff09\u2014\u2014 App \u7aef\u4e13\u7528<\/h1>\n\n\n\n<p>\u9002\u7528\u4e8e <strong>App \u7aef\u66f4\u590d\u6742\u7684\u901a\u4fe1<\/strong>\uff08\u5982\u591a\u9875\u9762\u4e4b\u95f4\u6d88\u606f\u4e92\u53d1\uff09\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udccc uni-app \u7aef\u76d1\u542c\u4e8b\u4ef6<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst currentWebview = this.$scope.$getAppWebview();\ncurrentWebview.addEventListener(&quot;customEvent&quot;, e =&gt; {\n  console.log(&quot;\u6536\u5230\u4e8b\u4ef6\uff1a&quot;, e.detail);\n});\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udccc H5 \u5411 App \u6d3e\u53d1\u4e8b\u4ef6<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ndocument.addEventListener(&quot;plusready&quot;, function() {\n  plus.webview.currentWebview().evalJS(`\n      uni.$emit(&#039;customEventFromH5&#039;, { msg: &#039;\u6765\u81eaH5\u7684\u4e8b\u4ef6&#039; });\n  `);\n});\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 \u65b9\u6cd5 3\uff1aJSBridge\uff08\u9ad8\u7ea7\u65b9\u5f0f\uff09<\/h1>\n\n\n\n<p>\u9002\u5408\u505a <strong>\u6df1\u5ea6\u539f\u751f\u6269\u5c55<\/strong> \u6216 <strong>\u8c03\u7528\u539f\u751f\u529f\u80fd<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udccc uni-app \u66b4\u9732\u65b9\u6cd5\u7ed9 H5 \u8c03\u7528<\/h2>\n\n\n\n<p>\u5199\u5728 uni-app \u9875\u9762\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nwindow.invokeFromH5 = function (data) {\n  console.log(&quot;H5 \u8c03\u7528\u4e86 APP \u65b9\u6cd5&quot;, data);\n};\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udccc H5 \u8c03\u7528\uff1a<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nwindow.invokeFromH5 &amp;amp;&amp;amp; window.invokeFromH5(&quot;hello&quot;);\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83c\udf1f \u6700\u5b8c\u6574\u53cc\u5411\u901a\u4fe1\u6848\u4f8b\uff08\u53ef\u76f4\u63a5\u7528\uff09<\/h1>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u6700\u5e38\u7528\u7684\u5b8c\u6574\u901a\u4fe1\u793a\u4f8b\uff1a<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udccc uni-app \u9875\u9762\uff1apages\/webview\/index.vue<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;template&gt;\n  &amp;lt;view class=&quot;content&quot;&gt;\n    &amp;lt;button @click=&quot;sendToH5&quot;&gt;\u53d1\u6d88\u606f\u7ed9H5&amp;lt;\/button&gt;\n\n    &amp;lt;web-view :src=&quot;url&quot; @message=&quot;receiveFromH5&quot;&gt;&amp;lt;\/web-view&gt;\n  &amp;lt;\/view&gt;\n&amp;lt;\/template&gt;\n\n&amp;lt;script setup&gt;\nimport { ref } from &quot;vue&quot;;\n\nconst url = ref(&quot;\/hybrid\/h5\/index.html&quot;);\n\n\/\/ \u63a5\u6536 H5 \u6d88\u606f\nfunction receiveFromH5(e) {\n  console.log(&quot;\u6536\u5230 H5 \u6d88\u606f\uff1a&quot;, e.detail.data);\n}\n\n\/\/ \u53d1\u6d88\u606f\u7ed9 H5\nfunction sendToH5() {\n  const webview = getCurrentPages().pop().$getAppWebview().children()&#x5B;0];\n  webview.postMessage({\n    msg: &quot;\u8fd9\u662f\u6765\u81ea uni-app \u7684\u6d88\u606f&quot;\n  });\n}\n&amp;lt;\/script&gt;\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udccc H5\uff1ahybrid\/h5\/index.html<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;!DOCTYPE html&gt;\n&amp;lt;html&gt;\n&amp;lt;body&gt;\n  &amp;lt;h3&gt;H5 \u9875\u9762&amp;lt;\/h3&gt;\n\n  &amp;lt;button onclick=&quot;sendToApp()&quot;&gt;\u53d1\u6d88\u606f\u7ed9 APP&amp;lt;\/button&gt;\n\n  &amp;lt;script&gt;\n    \/\/ H5 -&gt; uni-app\n    function sendToApp() {\n      uni.postMessage({\n        data: {\n          text: &quot;\u6765\u81ea H5 \u7684\u6d88\u606f&quot;\n        }\n      });\n    }\n\n    \/\/ uni-app -&gt; H5\n    window.addEventListener(&quot;message&quot;, function (event) {\n      console.log(&quot;\u6536\u5230\u6765\u81ea uni-app \u7684\u4fe1\u606f\uff1a&quot;, event.data);\n    });\n  &amp;lt;\/script&gt;\n&amp;lt;\/body&gt;\n&amp;lt;\/html&gt;\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udccc \u4e09\u79cd\u65b9\u6cd5\u5bf9\u6bd4\u8868<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u65b9\u6cd5<\/th><th>\u9002\u7528\u7aef<\/th><th>\u4f18\u70b9<\/th><th>\u7f3a\u70b9<\/th><\/tr><\/thead><tbody><tr><td><code>postMessage<\/code><\/td><td>App\u3001\u5c0f\u7a0b\u5e8f\u3001H5<\/td><td>\u5b98\u65b9\u63a8\u8350\u3001\u53cc\u5411\u901a\u4fe1\u3001\u6700\u7a33\u5b9a<\/td><td>\u4e0d\u80fd\u6267\u884c JS \u4ee3\u7801<\/td><\/tr><tr><td><code>plus.webview<\/code><\/td><td>\u4ec5 App<\/td><td>\u529f\u80fd\u5f3a\u3001\u53ef evalJS<\/td><td>\u53ea\u80fd App \u7528<\/td><\/tr><tr><td>JSBridge<\/td><td>App<\/td><td>\u7075\u6d3b\u53ef\u6269\u5c55<\/td><td>\u9700\u81ea\u5df1\u8bbe\u8ba1\u534f\u8bae<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u4e3a\u4f60\u6574\u7406\u4e00\u4efd uni-app \u4e2d WebView \u9875\u9762\u4e0e\u539f\u751f\u9875\u9762\/\u7236\u9875\u9762\u901a\u4fe1&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/05\/uni-app%e4%b9%8bwebview%e9%80%9a%e4%bf%a1%e5%ae%9e%e7%8e%b0%e6%96%b9%e6%b3%95%e7%a4%ba%e4%be%8b\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"class_list":["post-526","post","type-post","status-publish","format-standard","hentry","category-javascript"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/526","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=526"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/526\/revisions"}],"predecessor-version":[{"id":527,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/526\/revisions\/527"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}