

微信网页跳转小程序
source link: https://mrleidesen.github.io/posts/%E5%BE%AE%E4%BF%A1%E7%BD%91%E9%A1%B5%E8%B7%B3%E8%BD%AC%E5%B0%8F%E7%A8%8B%E5%BA%8F/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

微信网页跳转小程序
前言#
这几天在写微信 H5 的项目,有一个功能是网页跳转小程序,遇到了非常坑的问题,因此记录一下
坑点#
开始使用#
要跳转小程序,就要用到微信 SDK 的开放标签 wx-open-launch-weapp
我们需要在 jssdk
中注册
wx.config({
// ... 其他配置按照需求配置
openTagList: ["wx-open-launch-weapp"] // 填入这个玩意儿
})
正常使用下,我们就是这么用
<wx-open-launch-weapp
id="launch-btn"
username="gh_xxxxxxxx"
path="pages/home/index?user=123&action=abc"
>
<!-- script 标签不能省略 -->
<script type="text/wxtag-template">
<button>打开小程序</button>
</script>
</wx-open-launch-weapp>
顺便吐槽,username
这个字段是小程序的原始ID,太蠢了…
好,按照以上操作,最简单版本的跳转可以了,但是问题来了,如果你需要非常复杂的样式呢?
踩坑#
根据官方文档说明
如果所使用的标签允许提供插槽,由于插槽中模版的样式是和页面隔离的,因此需要注意在插槽中定义模版的样式。
意思是,写在这个标签里的模板样式,是引用不到外界的,你要么写内联样式,要么在 script
里写 style
标签。
所以当你需要在现成的一个按钮外加这么一个标签,那么你的样式可能就会混乱,甚至标签不见。
<style>
.btn {
color: red;
}
</style>
<wx-open-launch-weapp
id="launch-btn"
username="gh_xxxxxxxx"
path="pages/home/index?user=123&action=abc"
>
<!-- script 标签不能省略 -->
<script type="text/wxtag-template">
<style>
.btn-inner {
color: red;
}
</style>
<button class="btn btn-inner">打开小程序</button>
<!-- 无法引用到外界的 btn 样式,但可以引用内部的 btn-inner -->
</script>
</wx-open-launch-weapp>
React#
如果你是在使用 React
,那么可能会报错 not in JSX.IntrinsicElements
,可以在 type.d.ts
中定义一个
declare global {
namespace JSX {
interface IntrinisicElements {
'wx-open-launch-weapp': any
}
}
}
解决方法#
因为我使用的是 React
所以就按照 React
写法来写实例
主要思路是将这个开放标签当成一层 cover 覆盖在我们需要的样式上面
const WxOpenLaunchButton: FC = ({ children }) => {
return (
<div
style={{
position: "relative",
width: 100, // 宽高也可通过 props 传进来,这里只做演示
height: 100, // 外层宽高必须定义,不然内部的开放标签拿不到宽高
}}
>
{children}
<wx-open-launch-weapp
id="launch-btn"
username="gh_xxxxxxxx"
path="pages/home/index?user=123&action=abc"
style={{
width: "100%",
height: "100%",
position: "absolute",
top: 0,
left: 0,
}}
>
<!-- script 标签不能省略 -->
<script type="text/wxtag-template">
<div
style={{
width: "100%",
height: "100%",
position: "absolute",
top: 0,
left: 0,
opacity: 0, // 透明度 0 让其不可见但能点击
}}
/>
</script>
</wx-open-launch-weapp>
</div>
)
}
封装好之后,下次使用只需要引用这个组件就可以啦
const Page = () => {
return (
<WxOpenLaunchButton>
<div className="btn">Open WeApp</div>
</WxOpenLaunchButton>
)
}
总结#
微信这文档是真的坑,而且整这么一个玩意儿还不说清楚,还需要自己瞎摸索,不过好在摸索出来了。
Recommend
-
125
小程序支持跳转网页会带来什么影响 Original 花叔...
-
97
网页端「应用跳转」技术实现演变
-
35
继小程序灰度测试分享朋友圈刷屏后,滴滴滴,这... 闲着也...
-
40
主体要求: 仅开放给已认证的服务号 系统要求:微信版本要求为: 7.0.12 及以上。系统版本要求为: iOS 10.3 及以上、 Android 5.0 及以上 接入微信JS-SDK...
-
7
微信小程序不能跳转 App!阿里小程序入驻后也只能用微信支付? 为优化用户体验,平台将于 2021 年 5 月 20 日之后不再提供「小...
-
7
速接收:H5跳转微信小程序的三种方式 小程序头条 2022年5月26日 13:46...
-
12
盘点:微信小程序跳转H5操作教程 小程序头条,
-
9
盘点:微信小程序跳转其他小程序操作教程 小程序头条,
-
7
微信小程序跳转其他小程序操作教程 小程序头条 2022年7月19日 15:02...
-
8
在业务中接触了微信小程序,客户对引流用户非常在意,每次都会提该需求,经常做就存档一下。使用的小程序账户都是企业版非个人版本。在微信公众平台-小程序后台配...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK