angular - 如何从 Angular 14 中的库导入独立组件?

标签 angular angular14

Angular 14 引入了新的独立组件,不需要使用任何模块。如果在库中提供,如何使用这些组件?在标准的非独立组件中,我们首先必须导入给定的模块。 Angular 如何识别我正在导入的组件来自这个特定的包?

最佳答案

要制作独立组件,需要将组件定义为standalone在组件的装饰器中使用 standalone 参数,然后你可以使用 imports组件中的语句也是如此。您的组件将如下所示。

@Component({
  standalone: true,
  imports: [CommonModule],
  selector: 'example-component',
  template: `./example.component.html`,
})
export class ExampleComponent {}

接下来您需要将该组件导入到其他组件/模块中。您现在可以在 import 属性中将其导入到您的模块中,这在以前是不受支持的。或者您可以将它导入到另一个同样不受支持的组件中,现在是。

// Importing using a Module
@NgModule({
  imports: [ExampleComponent]
})
export class MyModule {}

// Importing using a component
// This component also needs the standalone property
@Component({
  standalone: true,
  imports: [ExampleComponent],
  selector: 'some-component',
  template: `./component.html`,
})
export class ExampleComponent {}

关于angular - 如何从 Angular 14 中的库导入独立组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72517141/

相关文章:

Angular ag-grid 单元格渲染器复选框不刷新值

Angular 14 - 错误 : Trying to publish a package that has been compiled by Ivy in full compilation mode

angular - 如何在 Angular 共享库中创建模型?

angular - 正确理解 angular14 中的注入(inject) - 必须从注入(inject)上下文中调用 inject()

angular - 如何将 ngclass 绑定(bind)到一个可观察值

angular - 为什么我的变量没有在 Typescript 中被指定为数组?

javascript - Cordova插件如何安装

javascript - 如何路由防范不存在的 url 路径?