gpt4 book ai didi

Angular 4 扩展基础组件

转载 作者:太空狗 更新时间:2023-10-29 17:04:23 25 4
gpt4 key购买 nike

我有一个由其子组件扩展的基础组件,但是当我们使用 angular-cli 在 Angular 中创建一个新组件时,它会创建 html 和 css 文件,但我不会使用基础组件中的这些文件。

有没有办法在没有 html 和 css 的情况下创建基础组件?

import { Component, OnInit, Input } from '@angular/core';

@Component({
selector: 'app-base',
templateUrl: './base.component.html', //I dont need this
styleUrls: ['./base.component.css'] ////I dont need this
})
export class BaseComponent implements OnInit {

constructor() {}

ngOnInit() {}

}


import { Component } from '@angular/core';

import { BaseComponent } from '../base/base.component';

@Component({
selector: 'app-child',
templateUrl: './child.component.html',
styleUrls: ['./child.component.css']
})
export class ChildComponent extends BaseComponent {

constructor() {
super();
}

ngOnInit() {
}

}

最佳答案

由于基类不需要自己实例化,它是抽象类,不需要@Component装饰器。

如果有依赖,子类有可能省略constructor而被继承,基类应该有@Injectable装饰器:

@Injectable()
export abstract class BaseComponent implements OnInit {
constructor(public dep: Dep) {}

ngOnInit() {}
}

@Component({
selector: 'app-child',
templateUrl: './child.component.html',
styleUrls: ['./child.component.css']
})
export class ChildComponent extends BaseComponent {
// dummy constructor can be omitted
/*
constructor(dep: Dep) {
super(dep);
}
*/
}

关于Angular 4 扩展基础组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49320759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com