我在返回一个订阅对象时遇到了下面的错误。
错误:-类型'Subscription‘缺少类型'typeof Subscription’的以下属性: prototype,空
我的代码是:
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();
}
}发布于 2020-05-23 01:57:57
嗨,伙计们,我弄错了,这只是我的订阅声明。代码应该是这样的.
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();
}
}https://stackoverflow.com/questions/61961040
复制相似问题