How to determine whether uniapp is a mini program?
With the development of mobile Internet, WeChat mini programs have become a popular choice for many enterprises and individual developers. At the same time, the emergence of uniapp has further accelerated the convenience of mini program and H5 page development. When using uniapp to develop small programs, sometimes it is necessary to determine whether the current environment is a small program or an H5 page based on different scenarios. This article will focus on how uniapp determines whether the current environment is a small program, and explains the principles and methods of determination.
1. How does uniapp determine whether the current environment is a mini program?
In uniapp, you can obtain mini program information through the uni.getSystemInfoSync() method. In the system information, uniapp provides us with a special field platform to identify the current environment. If the platform is "mp-weixin", then the WeChat applet is currently running. In addition, you can also determine whether it is on a platform such as Alipay applet. A code sample is as follows:
if (uni.getSystemInfoSync().platform === 'mp-weixin') { console.log('当前为微信小程序环境!'); } else { console.log('当前不是微信小程序环境!'); }
2. Judgment Principle
Uniapp determines whether the current environment is a small program by obtaining system information. The uni.getSystemInfoSync() method is an API that provides the ability to obtain system information. This API returns an object containing device information, including platform, screen width and height, pixel density, etc. Therefore, we only need to obtain the platform field information of the current environment through this API to determine whether the currently running platform environment is a small program.
3. Judgment method
In uniapp, judging whether the current environment is a small program can be achieved through the following methods:
Method 1: Through uni.getSystemInfoSync( ) method to obtain platform information to determine whether the current running environment is a mini program.
Method 2: Obtain the running environment through the uni.getEnv() method. If it is a small program, it will return "WEAPP" or "ALIPAY".
Method 3: Use the uni.getMenuButtonBoundingClientRect() method to determine whether it is in a mini program. If the returned information is not empty, the mini program is running.
Method 4: By determining whether the UniServiceJSBridge object exists, you can determine whether it is currently in the mini program.
Code sample:
// 方法一 if (uni.getSystemInfoSync().platform === 'mp-weixin') { console.log('当前为微信小程序环境!'); } else { console.log('当前不是微信小程序环境!'); } // 方法二 if (uni.getEnv("PLATFORM") === "WEAPP" || uni.getEnv("PLATFORM") === "ALIPAY") { console.log("当前运行环境为小程序"); } else { console.log("当前运行环境不是小程序"); } // 方法三 if (uni.getMenuButtonBoundingClientRect()) { console.log('当前为小程序环境!'); } else { console.log('当前不是小程序环境!'); } // 方法四 if (typeof UniServiceJSBridge !== 'undefined') { console.log('当前为小程序环境!'); } else { console.log('当前不是小程序环境!'); }
4. Summary
This article mainly introduces the method and principle of how uniapp determines whether the current environment is a small program. By understanding this knowledge, we can better judge the current operating environment during the development of small programs, and adapt and optimize for different platforms. At the same time, we also need to continue to learn and master the development skills of uniapp and improve our development capabilities to better achieve our development goals.
The above is the detailed content of How to determine whether uniapp is a mini program?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



This article details uni-app's local storage APIs (uni.setStorageSync(), uni.getStorageSync(), and their async counterparts), emphasizing best practices like using descriptive keys, limiting data size, and handling JSON parsing. It stresses that lo

This article details making and securing API requests within uni-app using uni.request or Axios. It covers handling JSON responses, best security practices (HTTPS, authentication, input validation), troubleshooting failures (network issues, CORS, s

This article details uni-app's geolocation APIs, focusing on uni.getLocation(). It addresses common pitfalls like incorrect coordinate systems (gcj02 vs. wgs84) and permission issues. Improving location accuracy via averaging readings and handling

This article compares Vuex and Pinia for state management in uni-app. It details their features, implementation, and best practices, highlighting Pinia's simplicity versus Vuex's structure. The choice depends on project complexity, with Pinia suita

The article details how to integrate social sharing into uni-app projects using uni.share API, covering setup, configuration, and testing across platforms like WeChat and Weibo.

This article explains uni-app's easycom feature, automating component registration. It details configuration, including autoscan and custom component mapping, highlighting benefits like reduced boilerplate, improved speed, and enhanced readability.

Article discusses using Sass and Less preprocessors in uni-app, detailing setup, benefits, and dual usage. Main focus is on configuration and advantages.[159 characters]

This article details uni.request API in uni-app for making HTTP requests. It covers basic usage, advanced options (methods, headers, data types), robust error handling techniques (fail callbacks, status code checks), and integration with authenticat
