简体   繁体   English

将派生类属性传递给 Angular 中的基类构造函数时出错

[英]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.

相关问题 使用继承将参数传递给angular 2/4中的组件的基类的构造函数时出错 - Error while passing parameters to the constructor of the base class of a component in angular 2/4 using inheritance 如何避免在TypeScript,Angular2的派生类中重复基类构造函数参数? - How to avoid repeating base class constructor parameters in derived class in TypeScript, Angular2? 使用装饰器中的prototype / Access基类属性访问派生类中的基类属性 - Accessing base class properties in derived class using prototype/Access base class properties in the decorator 将角度服务传递给类和基类 - Passing angular service to class and base-class 如何获取基类(Angular2 +)中的派生类名称? - How to get the derived class name in the base class (Angular2+)? Angular:如何将参数从派生组件类传递给基类 - Angular: How to pass arguments from derived component class to base class Angular 2在派生类中捕获可观察到的http错误 - Angular 2 catch observable http error in derived class Angular / Typescript:编写从基类派生的工厂返回类型 - Angular/Typescript: write a factory returning types derived from base class 无法从派生类访问/获取抽象(基)类属性的值 - TypeScript - Unable to access/get value of abstract (base) class properties from derived class - TypeScript Angular2类构造函数错误:没有Number的提供程序 - Angular2 class constructor error: no provider for Number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM