Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Communication Between Uni-app and Embedded Web-view Components

Tech 2

Parent-to-Child Communication

URL Parameter Passing

For small data payloads, embed parameters direct in the web-view URL.

Parent Component (Vue 3)

<template>
  <web-view src="/static/webpage.html?payload=demo"></web-view>
</template>

JavaScript Bridge Method

Use Uni-app's bridge script for complex data transfer.

Parent Component

<script setup>
import { ref, onMounted } from 'vue'

const webViewRef = ref(null)
let embeddedView = null

onMounted(() => {
  setTimeout(() => {
    const pages = getCurrentPages()
    embeddedView = pages[pages.length - 1].$getAppWebview().children()[0]
    embeddedView.evalJS('acceptData({"status": "active", "id": 42})')
  }, 1000)
})
</script>

Child Page (HTML)

<script src="/scripts/uni.webview.1.5.3.js"></script>
<script>
function acceptData(payload) {
  console.log('Received:', JSON.stringify(payload))
}
</script>

Child-to-Parent Communication

Parent Component

<template>
  <web-view src="/static/webpage.html" @message="processEvent"></web-view>
</template>

<script setup>
function processEvent(event) {
  console.log('Event data:', event.detail)
}
</script>

Child Page (HTML)

<button onclick="transmitInfo()">Send Data</button>
<script src="/scripts/uni.webview.1.5.3.js"></script>
<script>
function transmitInfo() {
  uni.postMessage({
    data: { action: "update", value: 78 }
  })
}
</script>

Available Methods

Method Description Platform Notes
navigateTo Open new page Universal
redirectTo Replace current page Universal
reLaunch Reset navigation stack Universal
switchTab Switch tabbar page Universal
navigateBack Return to previous page Universal
postMessage Send message to native ByteDance/H5 limited
getEnv Detect runtime environment ByteDance/Feishu excluded

Component Properties

Property Type Description Platform Notes
src String Target URL Universal
allow String Feature policy H5 only
sandbox String Security restrictions H5 only
webview-styles Object Styling options App-vue
update-title Boolean Auto-update title App-vue (HX 3.3.8+)
@message EventHandler Message receiver H5 limited
@onPostMessage EventHandler Real-time messaging App-nvue

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.