【发布时间】:2018-04-14 09:01:19
【问题描述】:
这是我的第一个 Angular 4 项目的一部分。我目前可以从搜索栏中很好地调用 searchCall 函数,但是存储在 tweetsData 中的数据似乎不在我在 app.component.html 中调用的 *ngFor 范围内,并且作为异步后端调用 twitter api。我收到此错误:TypeError: Cannot read property 'subscribe' of undefined,所以我不能正确设置 observable。非常感谢任何帮助。
twitter.service.ts
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
@Injectable()
export class TwitterService {
searchQuery: string = '';
tweetsData;
constructor(private http: Http) {
console.log('TwitterService created');
this.getBearerToken();
}
getBearerToken() {
// get bearer token from twitter api
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
this.http.post('http://localhost:3000/authorize', {headers: headers}).subscribe((res) => {
console.log(res);
});
}
getTweetsData():Observable<any> {
return this.tweetsData;
}
searchCall() {
console.log('searchCall made');
console.log(this.searchQuery);
var headers = new Headers();
var searchTerm = 'query=' + this.searchQuery;
headers.append('Content-Type', 'application/x-www-form-urlencoded');
this.http.post('http://localhost:3000/search', searchTerm, {headers: headers}).subscribe((res) => {
this.tweetsData = res.json().data.statuses;
});
}
}
app.component.ts
import { Component, OnInit, TemplateRef } from '@angular/core';
import { Http, Headers } from '@angular/http';
import { TwitterService } from './twitter.service';
import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: [ 'custom-styles/app.component.css' ],
providers: [TwitterService]
})
export class AppComponent implements OnInit {
searchQuery: string = '';
tweetsData;
expandedNewTweetBox: boolean = false;
public modalRef: BsModalRef;
constructor(private http: Http, private twitterService: TwitterService, private modalService: BsModalService) {}
ngOnInit() {
this.twitterService.getTweetsData().subscribe(
data => this.tweetsData = data
)
}
public openModal(template: TemplateRef<any>) {
this.modalRef = this.modalService.show(template);
}
}
app.component.html
...
<div *ngIf="tweetsData">
<div *ngFor="let item of tweetsData" class="col-12">
...
navbar.component.html
<div class="input-group">
<input type="text" class="form-control" placeholder="Search Chiller" aria-label="Search Chiller" [(ngModel)]="twitterService.searchQuery" [ngModelOptions]="{standalone: true}">
<span class="input-group-btn">
<button class="btn btn-secondary" type="button" (click)="twitterService.searchCall()">Go!</button>
</span>
</div>
【问题讨论】:
-
你永远不会打电话给
searchCall -
@Aluan Haddad 我愿意。该调用位于 app.component.html 的另一部分,我省略了该部分,因为我测试了对搜索函数的调用并且它按预期工作。
-
你不应该假设这样的缓存。你可能有比赛条件或其他什么。
-
抱歉,对 searchCall 的调用是在另一个组件“navbar.component”中进行的。我已经从该文件中添加了相关代码。希望这会有所帮助。
-
啊,我明白了问题,您在组件级别有
providers: [TwitterService],这意味着组件获得了一个具有自己状态的单独服务实例。这通常没问题,除了您的服务是有状态的并且期望是单例的。
标签: angular observable angular2-services