gpt4 book ai didi

Inherit another UI library selector properties in a component attribute selector(在组件属性选择器中继承另一个UI库选择器属性)

转载 作者:bug小助手 更新时间:2023-10-22 15:58:56 26 4
gpt4 key购买 nike



I have a buildable/publishable Angular library which I want to wrap ng-zorro button (and other components as well one-by-one) within to add my attribute selector instead of the ng-zorro selector and add more properties to the original component.

我有一个可构建/可发布的Angular库,我想将ng-zoro按钮(以及其他组件)一个接一个地封装在其中,以添加我的属性选择器,而不是ng-zorro选择器,并为原始组件添加更多属性。


In the source code of ng-zorro, you can see that the nz-button attribute is implemented like this:

在ng zorro的源代码中,您可以看到nz按钮属性是这样实现的:



@Component({
selector: 'button[nz-button], a[nz-button]',
exportAs: 'nzButton',
preserveWhitespaces: false,
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
template: `
<span nz-icon nzType="loading" *ngIf="nzLoading"></span>
<ng-content></ng-content>
`,
host: {
class: 'ant-btn',
'[class.ant-btn-primary]': `nzType === 'primary'`,
'[class.ant-btn-dashed]': `nzType === 'dashed'`,
// ...
}
})

As you can see it is not a @Directive but a @Component that selects elements with the condition button[nz-button].

正如您所看到的,它不是一个@Directive,而是一个用条件按钮[nz按钮]选择元素的@Component。


Now this is the component I developed to wrap that component:

现在,这是我为包装该组件而开发的组件:


@Component({
selector: 'button, a',
preserveWhitespaces: false,
imports: [
NzButtonModule
],
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
template: `<ng-content></ng-content>`,
styleUrls: ['./button.component.scss'],
standalone: true,
host: {
'nz-button': '',
class: 'btn',
'[class.btn-primary]': `color === 'primary'`,
'[class.btn-secondary]': `color === 'secondary'`,
// ...
}
})

export class ButtonComponent {
@Input() size: ButtonSize = 'md';
@Input() shape: ButtonShape = 'default';
@Input() color: ButtonColor = 'primary';
@Input() fill: ButtonFill = 'solid';
@Input() expand: ButtonExpand = 'default';
}

What I am trying to achieve is that the button tag I place in the consumer components has all the ng-zorro attributes and mine too so I can implement new attributes to the native tag and override the ant styles in some cases.

我试图实现的是,我放置在使用者组件中的按钮标签具有所有的ng-zorro属性,我也具有这些属性,这样我就可以为原生标签实现新的属性,并在某些情况下覆盖ant样式。


In other words, I want to have [nzType] (from ng-zorro) and shape (from my ButtonComponent) both on a <button> tag.

换句话说,我想让[nzType](来自ng-zarro)和shape(来自我的ButtonComponent)都在


更多回答

Could you create a StackBlitz?

你能创建一个StackBlitz吗?

优秀答案推荐

I ended up extending ng-zorro component to inherit its Inputs on my custom component it was not exactly what I wanted because now I have to use ng-zorro inputs by their name but not my desired name. For example, I have to use it like [nzSize] but not [size].

我最终扩展了ng-zorro组件,以继承其在我的自定义组件上的输入——这并不是我想要的,因为现在我必须使用ng-zorro-Inputs的名称,而不是我需要的名称。例如,我必须像[nzSize]那样使用它,但不能像[size]那样使用。


It is a workaround that will do the work but not a solid and complete solution.

这是一个可以完成工作的变通方法,但不是一个可靠而完整的解决方案。


I accept my solution until a better one comes.

我接受我的解决方案,直到更好的解决方案出现。


更多回答

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