1. Use weixin-js-sdk
npm install weixin-js-sdk
//在当前页面引入
import wx from "weixin-js-sdk";
2. Add the following code where you need to judge
var ua = window.navigator.userAgent.toLowerCase();
let that = this;
if (ua.match(/MicroMessenger/i) == "micromessenger") {
//微信环境下
wx.miniProgram.getEnv(function (res) {
if (res.miniprogram) {
// 小程序环境下逻辑
console.og("小程序环境下")
} else {
//非小程序环境下逻辑
console.log("非小程序环境下")
}
});
}