微信H5跳轉到小程序


需求

  • 需要從微信的H5網頁進入我們自己的小程序。

實現

  • 步驟,參見https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_Open_Tag.html官方文檔去加載對應的開放標簽。
  • 由於框架的問題,會導致在vue和react中,加載不出來該開放標簽,所以需要做特殊處理。

react處理

  • 將開放標簽的代碼寫成字符串,然后傳給一個空的divdangerouslySetInnerHTML屬性。
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里面這樣寫應該也是可以的,不過既然官方提供了原生的插入htmlapi就沒必要用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;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM