Ionic2 + Angular2将配置传递给依赖项

时间:2016-02-18 05:57:27

标签: angularjs dependency-injection ionic-framework angular ionic2

我想设置全局配置文件或来自app.ts

传递配置,可以在我们的依赖注入上自动使用

Api.ts服务

import {Injectable} from 'angular2/core';
import {Http} from 'angular2/http'

@Injectable()
export class Api {

    constructor(private http: Http) {}

    login (username, password)
    {
        return this.http.get(`../?client_id=${clientId}&client_secret=${clientSecret}&grant_type=password&username=${username}&password=${password}`).map(res => res.json());
    }
}

如何从typescript angular2?

全局传递客户端ID和客户端密钥到api依赖关系

1 个答案:

答案 0 :(得分:1)

您可以使用凭据创建文件

credentials.ts

export var credentials = {
  client_id: 1234,
  client_secret: 'secret'
}

并将其导入您的文件

import {Injectable} from 'angular2/core';
import {Http} from 'angular2/http'
import {credentials} from 'credentials'

@Injectable()
export class Api {

    constructor(private http: Http) {}

    login (username, password)
    {
        return this.http.get('../?client_id=' + credentials.client_id + '&client_secret=' + credentials.client_secret + '&grant_type=password&username=${username}&password=${password}`).map(res => res.json());
    }
}