当前位置:网站首页>How web pages interact with applets
How web pages interact with applets
2022-07-04 09:35:00 【Dandelion_ drq】
summary
The interaction and communication between web pages and applets need to be used in applets web-view
Component open web page , And the visited web pages need to introduce wechat js-sdk
, adopt js-sdk
Provide an interface to send information to the applet .
For details, please refer to the official documentation of the applet :https://developers.weixin.qq.com/miniprogram/dev/component/web-view.html
Applet background configuration business domain name
This is the limitation of applet development , stay web-view
The domain name of the web page to be opened in the wechat public platform applet background Development
--> Development and setup
--> Business domain name
Add an item to , otherwise web-view
Unable to open this page .
PS: If you don't want to configure this, you can also do it in WeChat developer tools
Open inside Do not verify legal domain name
.
Web page introduction WeChat js-sdk
WeChat JS-SDK
It is a web page development kit based on wechat provided by wechat . Students who have done wechat official account web page development should know ~~( That's notoriously hard to use ……)~~.
Official document link :https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html
I won't write about how to introduce , Just follow the instructions in the above document .
Want to use npm You can also find a third party if you quote in this way , I use this weixin-js-sdk.
Web pages send messages to applets
I made a very simple demo page , Demonstrates the function of navigating to the applet page and sending messages to the applet .
The page is as follows :
Page is vue
Written , It's simple , Go straight to the code :
<template>
<section>
<section>
<h2> Click picture event </h2>
<div @click="onClick">
<img src="https://via.placeholder.com/300x150" alt="">
</div>
</section>
<section>
<h2> Interact with applets </h2>
<button @click="switchTab"> Go back to the homepage of the applet </button>
<button @click="postMessage"> Send messages to applets </button>
</section>
</section>
</template>
<script> const wx = require('weixin-js-sdk'); export default {
data () {
return {
isMiniprogram: false, // Is it an applet environment }; }, mounted () {
this.wx = wx; this.wx.miniProgram.getEnv((res) => {
console.log('getEnv', res, res.miniprogram); this.isMiniprogram = true; }); }, methods: {
onClick () {
console.log(' Click on the image ', this.wx); }, switchTab () {
if (this.isMiniprogram) {
this.wx.miniProgram.switchTab({
url: '/pages/index/index', success: function (res) {
console.log('success', res); }, fail: function (err) {
console.log('fail', err); }, complete: function (res) {
console.log('complete', res); }, }); } }, postMessage () {
if (this.isMiniprogram) {
this.wx.miniProgram.postMessage({
data: {
foo: 'bar' }, }); } }, }, }; </script>
<style lang="scss" scoped> button {
padding: 10px; background: #333; color: #fff; border: none; margin-right: 10px; border-radius: 5px; } h2 {
padding: 30px 0 20px 0; } section {
text-align: center; } </style>
Here are a few points to pay attention to :
When navigating to the applet page, the routing method is the same as that of applet development , That is, if you want to navigate to
tabbar
page , Out-of-servicenavigateTo
Method , UseswitchTab
, Otherwise, no jump will occur during applet debugging , And there was no error message .( This is where I got stuck ……)wx.miniProgram.postMessage
The parameter format of method is object , And there must bedata
attribute , namely{data:...}
such , Otherwise, you won't receive it in the applet .WeChat
js-sdk
Official documents say that you need to configure permissions before using all interfaces , However !!miniProgram
This series of methods can be dispensed withconfig
Of …… Someone asked about this community : Applet jump page yes bug Do you . Because I don't want to get wechat user information on this page , So this is still more important for me .
The applet receives messages
Applet web-view
Of bindmessage
It will not be received immediately after the web page sends the message , Instead, it will retreat in the applet 、 Component destruction 、 Trigger and receive a message when sharing . And if the web page sends multiple messages , When the applet receives data
Merge .
Page to applet postMessage when , At a certain time ( Applet back 、 Component destruction 、 Share ) Trigger and receive a message .e.detail = { data },data Many times postMessage An array of parameters of
Look directly at the code and output
Code :
<!-- wxml -->
<view class='web-view'>
<web-view src="{
{url}}" bindmessage="getMsgFromWeb" bindload="onWebLoad" binderror="onWebError"></web-view>
</view>
// js
getMsgFromWeb(e) {
console.log('getMsgFromWeb', e.detail.data)
},
onWebLoad(e) {
console.log('onWebLoad', e)
},
onWebError(e) {
console.error('onWebError', e)
}
Output :
The screenshot above is that I clicked 10 Send Message button again , then getMsgFromWeb
The output of is output after I click the button to return to the home page of the applet .
End
This is the basic way to realize the interactive communication between web pages and applets , Because I was cheated when I developed it , Make a record here , I hope it is useful for students who are still climbing the pit ~~
边栏推荐
- 165 webmaster online toolbox website source code / hare online tool system v2.2.7 Chinese version
- [on February 11, 2022, the latest and most fully available script library collection of the whole network, a total of 23]
- Review of last week's hot spots (6.27-7.3)
- About the for range traversal operation in channel in golang
- What is permission? What is a role? What are users?
- Are there any principal guaranteed financial products in 2022?
- Report on the development trend and prospect trend of high purity zinc antimonide market in the world and China Ⓕ 2022 ~ 2027
- Write a jison parser from scratch (3/10): a good beginning is half the success -- "politics" (Aristotle)
- DR6018-CP01-wifi6-Qualcomm-IPQ6010-IPQ6018-FAMILY-2T2R-2.5G-ETH-port-CP01-802-11AX-MU-MIMO-OFDMA
- Global and Chinese markets of hemoglobin analyzers in care points 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
2022-2028 global gasket plate heat exchanger industry research and trend analysis report
自动化的优点有哪些?
2022-2028 global edible probiotic raw material industry research and trend analysis report
How should PMP learning ideas be realized?
Mac platform forgets the root password of MySQL
Sword finger offer 30 contains the stack of Min function
pcl::fromROSMsg报警告Failed to find match for field ‘intensity‘.
Leetcode (Sword finger offer) - 35 Replication of complex linked list
After unplugging the network cable, does the original TCP connection still exist?
2022-2028 global strain gauge pressure sensor industry research and trend analysis report
随机推荐
Tkinter Huarong Road 4x4 tutorial II
If you can quickly generate a dictionary from two lists
PHP student achievement management system, the database uses mysql, including source code and database SQL files, with the login management function of students and teachers
2022-2028 global visual quality analyzer industry research and trend analysis report
Daughter love in lunch box
2022-2028 global intelligent interactive tablet industry research and trend analysis report
Analysis report on the development status and investment planning of China's modular power supply industry Ⓠ 2022 ~ 2028
Target detection -- intensive reading of yolov3 paper
The child container margin top acts on the parent container
《网络是怎么样连接的》读书笔记 - Tcp/IP连接(二)
Trim leading or trailing characters from strings- Trim leading or trailing characters from a string?
pcl::fromROSMsg报警告Failed to find match for field ‘intensity‘.
C language - Introduction - Foundation - syntax - [main function, header file] (II)
Global and Chinese market of sampler 2022-2028: Research Report on technology, participants, trends, market size and share
How do microservices aggregate API documents? This wave of show~
2022-2028 global special starch industry research and trend analysis report
SSM online examination system source code, database using mysql, online examination system, fully functional, randomly generated question bank, supporting a variety of question types, students, teache
什么是uid?什么是Auth?什么是验证器?
Service call feign of "micro service"
Are there any principal guaranteed financial products in 2022?