1

我想设置全局配置文件或从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 依赖项?

4

1 回答 1

1

您可以使用您的凭据创建文件

凭据.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());
    }
}
于 2016-02-18T07:30:40.727 回答