【问题标题】:angular2/4 cant find objectangular2/4 找不到对象
【发布时间】:2017-05-09 17:34:01
【问题描述】:

为什么我找不到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...

【问题讨论】:

    标签: angular typescript rxjs


    【解决方案1】:

    您可能是指Object 带有大写O。 JavaScript / Typescript 区分大小写。

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

    【讨论】:

      【解决方案2】:

      你必须使用&lt;any&gt;

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

      在这里您可以找到anydocumentation,在示例代码中您将看到使用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
      }
      

      【讨论】:

      • 谢谢!。我是 angular 2 的新手,但之前在 angular1 上做过一些项目。我们可以谈谈吗?
      • 你有更多讨论的渠道吗?
      • 由于我的 150 代表输给了另一个主题赏金,我得到以下信息:您需要 20 声望才能发布消息
      • @maria 表示您需要更多声望才能聊天。无论如何,如果您在开发过程中遇到更多问题,请继续在 StackOverflow 中创建新问题,我们将为您提供帮助。如果您愿意,可以标记我,以便我可以看到问题并尝试帮助您。但真的不要犹豫使用 SO 的平台,这是通过新语言/框架学习路径的最快方法之一。 ;)
      • 我相信我标记了你知道。标记为sraxi
      【解决方案3】:

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

      欲了解更多信息,请参阅:What's-new-in-TypeScript#object-type

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-14
        • 2018-10-20
        • 2019-01-12
        • 1970-01-01
        相关资源
        最近更新 更多