[英]Error on passing derived class properties to the base class constructor in Angular
I want to be able to pass 'name' variable from the Derived component to the constructor of the Base Component.我希望能够将“名称”变量从派生组件传递给基础组件的构造函数。 The reason being I want to enforce Derived components extending from the Base components to pass in the 'name' variable
原因是我想强制从 Base 组件扩展的派生组件传入“name”变量
@Component({
selector: 'base-component',
templateUrl: './base.component.html',
styleUrls: ['./base.component.scss']
})
export class BaseComponent {
public name: string;
constructor(public sampleName: string) {
this.name = sampleName;
}
}
I'm importing the 'name' variable from a separate file called name.enum.ts in my Derived component我从派生组件中名为 name.enum.ts 的单独文件中导入“name”变量
export enum Name {
NAME = 'DummyName'
}
This is my derived component这是我的派生组件
import {Name} from ../name.enum
@Component({
selector: 'derived-component',
templateUrl: './derived.component.html',
styleUrls: ['./derived.component.scss']
})
export class DerivedComponent extends BaseComponent {
constructor() {
super(Name.NAME); //This throws an error 'Cant resolve all parameters for BaseComponent'
}
How do I resolve this issue?我该如何解决这个问题?
Your base class shouldn't be a component, but an abstract class.你的基类不应该是一个组件,而应该是一个抽象类。 Try changing it to
尝试将其更改为
export abstract class BaseComponent {
public name: string;
constructor(simpleName: string) {
this.name = simpleName;
}
}
Here is a live example: https://stackblitz.com/edit/angular-base-component?file=src/app/app.component.ts这是一个活生生的例子: https : //stackblitz.com/edit/angular-base-component?file=src/app/app.component.ts
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.