angular2 / 4无法找到物体

时间:2017-05-09 09:09:16

标签: angular typescript rxjs

为什么我找不到object

我收到以下错误:

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...

3 个答案:

答案 0 :(得分:2)

您可能认为Object的资本 O 。 JavaScript / Typescript区分大小写。

userChange: Subject<Object> = new Subject<Object>();

答案 1 :(得分:1)

您必须使用<any>

userChange: Subject<any> = new Subject<any>();

在这里,您可以找到any documentation,在示例代码中,您将看到使用Objectany之间的区别。

我会以这种方式处理您的代码(个人偏好)

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
}

答案 2 :(得分:0)

在TypeScript 2.2中添加了object类型,您确定使用的是正确的TypeScript版本吗?

有关详细信息,请参阅:What's-new-in-TypeScript#object-type