首页 > 解决方案 > 识别是否部署在移动应用程序或 Web 应用程序上的环境变量?

问题描述

嗨,我正在使用带有 Angular 的 Ionic 5。我有一个移动应用程序,我目前正在为其开发一个 Web 应用程序,它使用与移动应用程序相同的代码库。

我正在寻找一种方法来配置一些环境变量以识别应用程序是部署在移动应用程序还是 Web 应用程序上。谢谢 :)

标签: node.jsangularjsionic-frameworkionic5

解决方案


import {Platform } from '@ionic/angular';

@Component({
templateUrl: 'app.html'
})
export class MyApp {
    rootPage = HomePage;

    constructor(platform: Platform) {
        platform.ready().then(() => {

            if (this.platform.is('android')) {
                console.log("running on Android device!");
            }
            if (this.platform.is('ios')) {
                console.log("running on iOS device!");
            }
            if (this.platform.is('mobileweb')) {
                console.log("running in a browser on mobile!");
            }

        });
    }
}

推荐阅读