angularjs - Angular 1 到 Angular 5(导入嵌套组件)

标签 angularjs angular angular5

之前

  • Angular 1.5
  • 用户界面路由器

现在

  • Angular 5.0

如何在像 angular 1.5 这样的组件父亲中导入子组件? 我从 angular 5 开始学习英雄教程,但没有解释这个转换,所有组件都导入到 app.modole.ts 中。

下一张图片尝试解释一下我如何在 angular 1.5 中导入

enter image description here

有人知道如何在新的 Angular 5 的组件中导入子组件吗? 还是绝对有必要导入 app.module.ts 中的所有组件?

最佳答案

它在任何方面都与 AngularJS 没有什么不同。这可以通过单个 App 模块(这对于复杂的应用程序来说不够)或模块层次结构来完成。

在 AngularJS 中:

angular.module('app', ['foo'])
.component('app', {...});

angular.module('foo', [])
.component('foo', {...})
.component('barUsedByFoo', {...});

在 Angular 中:

@NgModule({ declarations: [FooComponent, BarUsedByFooComponent], exports: [FooComponent] })
class FooModule {}

@NgModule({ imports: [FooModule], declarations: [AppComponent], bootstrap: [AppComponent] })
class AppModule {}

Angular团队提出了feature modules的概念.功能模块应使用 imports 明确指定其对其他模块的依赖性.所有模块组件都在 declarations 中指定和(可选)exports .

与 AngularJS 的不同之处在于 Angular 模块允许具有本地组件。如果BarComponentbar选择器在 declarations 中指定和 exports ,它将在所有组件模板中可用。如果BarComponent仅在 declarations 中指定而不是 exports ,它将仅在该模块的组件模板中可用。这样不同的模块可以有不同的<bar>不会污染全局命名空间。

如果BarComponent是路由组件,在declarations中都要指定和 exports ,因为这样它将对路由器模块可用。

关于angularjs - Angular 1 到 Angular 5(导入嵌套组件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48844291/

相关文章:

javascript - Angular2 检测 OnChange

启用 Ivy 的 Angular 库无法加载

angular - Angular5 中具有动态内容的可重用模态

Angular5 TestBed useValue 似乎没有注入(inject)相同的对象实例

AngularJS 在 View 中用初始值声明变量?

javascript - 如何将数组值迭代到另一个数组的所有元素

javascript - 如何强制 URL 参数出现在 ui-router 的 URL 中?

javascript - 在指令中调用 $compile($element)($scope) 的目的是什么

Angular 2 aot 无法绑定(bind)到 X,因为它不是 Y 的已知属性

javascript - Angular 中的随机动画