暴露独立组件的依赖关系

huangapple go评论96阅读模式
英文:

Expose dependencies of a standalone component

问题

在stackblitz上的重现

所有组件都是独立的。

我有一个使用输入指令的表单字段组件。我想在导入组件时导入该指令,而不必显式导入它。有点像模块的工作方式,但在独立模式下。

是否可能,如果是的话,如何?

  1. @Component({
  2. selector: 'my-app',
  3. standalone: true,
  4. imports: [CommonModule, FieldComponent, InputDirective], // ← 从这里移除指令
  5. template: `
  6. <app-field>
  7. <input type="text" placeholder="Some text">
  8. </app-field>
  9. `,
  10. })
  1. @Component({
  2. selector: 'app-field',
  3. standalone: true,
  4. imports: [InputDirective],
  5. template: '<ng-content select="input"></ng-content>',
  6. styleUrls: ['./field.component.css'],
  7. })
  1. @Directive({
  2. selector: 'app-field input',
  3. standalone: true,
  4. })
  5. export class InputDirective {
  6. constructor() {
  7. console.log('Here I am');
  8. }
  9. }
英文:

Reproduction on stackblitz

All the components are standalone.

I have a form-field component that uses an input directive. I would like to import the directive when I import the component, without having to explicitly import it. Kind of like how modules work, except in standalone mode.

Is it possible, and if so, how ?

  1. @Component({
  2. selector: &#39;my-app&#39;,
  3. standalone: true,
  4. imports: [CommonModule, FieldComponent, InputDirective], // ← Remove the directive from here
  5. template: `
  6. &lt;app-field&gt;
  7. &lt;input type=&quot;text&quot; placeholder=&quot;Some text&quot;&gt;
  8. &lt;/app-field&gt;
  9. `,
  10. })
  1. @Component({
  2. selector: &#39;app-field&#39;,
  3. standalone: true,
  4. imports: [InputDirective],
  5. template: &#39;&lt;ng-content select=&quot;input&quot;&gt;&lt;/ng-content&gt;&#39;,
  6. styleUrls: [&#39;./field.component.css&#39;],
  7. })
  1. @Directive({
  2. selector: &#39;app-field input&#39;,
  3. standalone: true,
  4. })
  5. export class InputDirective {
  6. constructor() {
  7. console.log(&#39;Here I am&#39;);
  8. }
  9. }

答案1

得分: 0

以下是翻译好的部分:

唯一的解决方法是对这种类型的导入(服务也有类似的用例)使用数组:

  1. export const FIELD_IMPORTS = [FieldComponent, InputDirective];
  2. @Component({
  3. selector: 'my-app',
  4. standalone: true,
  5. imports: [CommonModule, ...FIELD_IMPORTS],
  6. template: `
  7. <app-field>
  8. <input type="text" placeholder="Some text">
  9. </app-field>
  10. `,
  11. })
英文:

The only workaround for this type of imports (there are similar use-cases with services) that I have found is to use an array:

  1. export const FIELD_IMPORTS = [FieldComponent, InputDirective];
  2. @Component({
  3. selector: &#39;my-app&#39;,
  4. standalone: true,
  5. imports: [CommonModule, ...FIELD_IMPORTS],
  6. template: `
  7. &lt;app-field&gt;
  8. &lt;input type=&quot;text&quot; placeholder=&quot;Some text&quot;&gt;
  9. &lt;/app-field&gt;
  10. `,
  11. })

答案2

得分: 0

我刚刚看到了来自Alex Rickabaugh(angular团队成员)的tweet about this

> 简短回答:不要这样做 - standalone的目的是依赖项是单独导入的,而不是捆绑在一起。
>
> 细微的回答:如果您有一些一起工作并需要一起添加的指令,请使用NgModule。
>
> 技术性回答:[HlmAccordion,...] as const应该有效。

所以在你的情况下,最好坚持使用ngModules。

英文:

I just saw a tweet about this from Alex Rickabaugh (member of the angular team) :

> Short answer: don't - the point of standalone is that deps are
> imported individually and not bundled.
>
> Nuanced answer: if you have directives that work together and need to
> be added together, use an NgModule.
>
> Technical answer: [HlmAccordion, ...] as const should work

So in your case you're better of sticking to ngModules.

huangapple
  • 本文由 发表于 2023年7月27日 21:23:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780214.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定