需求
實現
- 步驟,參見
https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_Open_Tag.html
官方文檔去加載對應的開放標簽。
- 由於框架的問題,會導致在vue和react中,加載不出來該開放標簽,所以需要做特殊處理。
react處理
- 將開放標簽的代碼寫成字符串,然后傳給一個空的
div
的dangerouslySetInnerHTML
屬性。
const createWeAppHtml = () => {
return {
__html: `<wx-open-launch-weapp
id="launch-btn"
username="gh_xxxxxxxx" // 這個是小程序的原始ID,直接去小程序的基本設置頁面翻到最下面就能看到
path="pages/index/index.html"
>
<template>
<style>
.btn {
padding: 18px
}
</style>
<button class="btn">打開小程序</button>
</template>
</wx-open-launch-weapp>`
}
}
<div className="btn" dangerouslySetInnerHTML={createWeAppHtml()}></div>
vue
- 由於這個開放標簽是支持動態加載的,所以只需要寫成字符串然后動態的插入在對應的標簽里面就可以了。
react
里面這樣寫應該也是可以的,不過既然官方提供了原生的插入html
的api
就沒必要用dom
操作。
Mounted() {
const html = `
<wx-open-launch-weapp
id="launch-btn"
username="gh_xxxxxxxx" // 這個是小程序的原始ID,直接去小程序的基本設置頁面翻到最下面就能看到
path="pages/index/index.html"
>
<template>
<style>
.btn {
padding: 18px
}
</style>
<button class="btn">打開小程序</button>
</template>
</wx-open-launch-weapp>
`;
document.getElementById('slot-demo').innerHTML = html;
}