如何使用Ionic3中的import导出带有逻辑的const

时间:2017-07-21 16:58:29

标签: angular typescript ionic-framework

我正在构建一个ionic3应用程序并遇到设置代理的问题。在Browser中,ionic会识别我的proxyUrl的路径,如下所示。

ionic.config.json

{
  "name": "myApp",
  "app_id": "",
  "v2": true,
  "typescript": true,
  "proxies": [
    {
      "path": "/api",
      "proxyUrl": "https://www.example.net/api"
    }
  ]
}

但是,我遇到了离子会识别我的代码中的路径/api的问题,例如在get call this.http.get('/api')...中,但是无法识别模拟器或设备上的路径。

所以要修复我尝试执行以下操作,如果我在浏览器(mobileweb)中设置路径/api,否则设置我想要的URL。

我-config.ts

import { Platform } from 'ionic-angular';

export const api = (Platform.is('mobileweb')) ? "/api" : "https://www.example.net/api";

这不会起作用,因为我无法在此直接访问Platform

is(...) [ts] Property 'is' does not exist on type 'typeof Platform'.

上发出错误

我可以通过检查我所在的平台以不同的方式实现此目的以导出我想要的网址吗?

更新

由于错误的url:

,我尝试了以下返回false,因为我在模拟器中遇到错误

返回false

import { Platform } from 'ionic-angular';

let platform = new Platform();

export const api = (Platform.is('mobileweb')) ? "/api" : "https://www.example.net/api";

要在构造函数中测试,它返回true。

返回true

export class Test {

    constructor(platform: Platform){
      console.log('InBrowser', platform.is('mobileweb'));
    }
}

1 个答案:

答案 0 :(得分:0)

我-config.ts

SqlComm.CommandText = @"SELECT * FROM MyDataTable WHERE 
                        DataDate >= @minDate AND 
                        DataDate <= @maxDate";

// Example dtpSrcDataDate.Value = 23/07/2017 00:00:00
SqlComm.Parameters.AddWithValue("@minDate",dtpSrcDataDate.Value);
// add one day to get 24/07/2017 00:00:00 so you can search the whole 23th day
SqlComm.Parameters.AddWithValue("@maxDate",dtpSrcDataDate.Value.AddDays(1));

用法

import { Injectable } from '@angular/core';
import { Platform } from 'ionic-angular';

@Injectable()
export class GlobalProvider {
    public api: any;
    constructor(platform: Platform){
       this.api = (platform.is('mobileweb')) ? "/api" :"https://www.example.net/api";
    }
}