【发布时间】:2017-05-11 04:50:38
【问题描述】:
您好,我是 AngularJs 2 的新手。
我正在创建简单的应用程序来读取 Github API。
当我使用以下文件启动应用程序时,我收到以下错误:
AppComponent.html:1 ERROR TypeError: Cannot read property 'subcribe' of undefined
以下是我的服务和组件 ts 文件。
github.service.ts
import {Injectable} from '@angular/core';
import {Http, Headers} from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class GithubService{
private username: string;
private client_id = "client_id";
private client_secret = "client_secret";
constructor( private _http: Http){
console.log("Github service is ready...");
this.username = "graphicsmanoj";
}
getUser(){
this._http.get('http://api.github.com/users/'+this.username+'?client_id='+this.client_id+'&client_secret='+this.client_secret).map(res => res.json());
}
}
profile.component.ts
import { Component } from '@angular/core';
import { GithubService } from '../services/github.service';
@Component({
moduleId: module.id,
selector: 'profile',
templateUrl: 'profile.component.html',
})
export class ProfileComponent {
constructor(private _githubService: GithubService){
this._githubService.getUser().subcribe(user => {
console.log(user);
})
}
}
非常感谢您提前提供的解决方案。
【问题讨论】:
-
@Momin 请记住,当您过去代码时,请删除敏感信息,例如 client_id 和 client_secret。我现在已经为你删除了,但请记住这一点。
-
好的!但我在哪里做呢?
-
@Momin 您在此问题中编写的代码包含“client_id”和“client_secret”。写问题时要小心,不要包含任何敏感信息。我建议你去github.com/settings/tokens删除旧的并创建一个新的。
标签: angularjs node.js angular2-services