While developing an app using ionic3, I encountered an issue with setting up a proxy. When running in a browser, Ionic was able to recognize the path of my proxyUrl as shown below.
ionic.config.json
{
"name": "myApp",
"app_id": "",
"v2": true,
"typescript": true,
"proxies": [
{
"path": "/api",
"proxyUrl": "https://www.example.net/api"
}
]
}
However, I faced a problem where Ionic would identify the path /api
in my code (e.g., this.http.get('/api')...
) but not on the emulator or device.
To address this, I attempted the following approach: if the platform is a browser (mobileweb), set the path to /api
; otherwise, use the desired URL.
my-config.ts
import { Platform } from 'ionic-angular';
export const api = (Platform.is('mobileweb')) ? "/api" : "https://www.example.net/api";
Unfortunately, this method failed because I couldn't directly access Platform
in this context.
An error occurred at is(...)
, stating
[ts] Property 'is' does not exist on type 'typeof Platform'.
Is there an alternative way to implement this and export the URL based on the platform?
UPDATE
I experimented with the following code that resulted in false due to errors in the emulator caused by an incorrect URL:
Returns false
import { Platform } from 'ionic-angular';
let platform = new Platform();
export const api = (Platform.is('mobileweb')) ? "/api" : "https://www.example.net/api";
In contrast, when testing in the constructor, it returned true.
Returns true
export class Test {
constructor(platform: Platform){
console.log('InBrowser', platform.is('mobileweb'));
}
}