angular2/4找不到对象
问题描述:
为什么我找不到object
?angular2/4找不到对象
我收到以下错误:
core.service.ts (19,23): Cannot find name 'object'.
在行:
userChange: Subject<object> = new Subject<object>();
我确实有这些进口:
import { Injectable } from '@angular/core';
import { Http, RequestOptions, URLSearchParams } from '@angular/http';
import {Observable, } from 'rxjs/Observable';
import { Comment } from './models/comment'
import 'rxjs/add/operator/map';
import 'rxjs/Rx';
import {Subject} from 'rxjs/Subject';
import { Router, NavigationEnd, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
@Injectable()
export class CoreService {
constructor(private http: Http, private router: Router) { }
userChange: Subject<object> = new Subject<object>();
further...
答
你可能是指Object
大写Ø 。 JavaScript/Typescript区分大小写。
userChange: Subject<Object> = new Subject<Object>();
答
你必须使用<any>
。
userChange: Subject<any> = new Subject<any>();
这里您可以找到any
documentation,在示例代码,你会看到使用Object
和any
之间的差异。
我会接近你的代码这样(个人喜好):
userChange$: Observable<any>; // We declare the Observable
private userChangeSubject = new Subject<any>(); // We declare the Subject
constructor(private http: Http, private router: Router) {
this.userChange$ = this.userChangeSubject.asObservable(); // We 'associate' the Subject with the Observable
}
updateUser(someUserParams) {
this.userChangeSubject.next(someUserParams); // We then proceed to apply our logic and anyone subscribe to 'userChange$' will receive these someUserParams when this method is triggered
}
感谢! Im新增角2,但在angular1之前已经完成了一些项目。我们可以谈谈吗? – maria
@maria当然,我来帮忙! :D – SrAxi
您是否有渠道进行更多讨论? – maria