首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“'typeof”:原型,空

“'typeof”:原型,空
EN

Stack Overflow用户
提问于 2020-05-23 01:55:31
回答 1查看 102关注 0票数 1

我在返回一个订阅对象时遇到了下面的错误。

错误:-类型'Subscription‘缺少类型'typeof Subscription’的以下属性: prototype,空

我的代码是:

代码语言:javascript
复制
import { ActivatedRoute, Params } from '@angular/router';
import { Subscription } from 'rxjs/Subscription';
import { Component, OnInit, OnDestroy } from '@angular/core';

@Component({
  selector: 'app-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.css']
})
export class UserComponent implements OnInit, OnDestroy {
  user :{id:number, name:string};

  private paramSubscription = Subscription;

  constructor(private route:ActivatedRoute) { }

  ngOnInit(): void {
    this.user={
    id: this.route.snapshot.params['id'],
    name: this.route.snapshot.params['name']
    };
     this.paramSubscription = this.route.params.
     subscribe(
       (params:Params) => {
         this.user.id=params['id'],
         this.user.name=params['name']
       }
     );
  }

  ngOnDestroy(){
    this.paramSubscription.unsubscribe();
  }

}
EN

回答 1

Stack Overflow用户

发布于 2020-05-23 01:57:57

嗨,伙计们,我弄错了,这只是我的订阅声明。代码应该是这样的.

代码语言:javascript
复制
import { ActivatedRoute, Params } from '@angular/router';
import { Subscription } from 'rxjs/Subscription';
import { Component, OnInit, OnDestroy } from '@angular/core';

@Component({
  selector: 'app-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.css']
})
export class UserComponent implements OnInit, OnDestroy {
  user :{id:number, name:string};

  private paramSubscription:Subscription;

  constructor(private route:ActivatedRoute) { }

  ngOnInit(): void {
    this.user={
    id: this.route.snapshot.params['id'],
    name: this.route.snapshot.params['name']
    };
     this.paramSubscription = this.route.params.
     subscribe(
       (params:Params) => {
         this.user.id=params['id'],
         this.user.name=params['name']
       }
     );
  }

  ngOnDestroy(){
    this.paramSubscription.unsubscribe();
  }

}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61961040

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档