Angular2入门之新建模块与组件

Angular2新建模块和其中的组件的需要注意一些问题。
  本文新建一个LayoutModule模块,目录如下图:


新建layout模块的文件目录.png

1. 首先,创建文件夹如图中所示

+layout
+++main-layout
+++navigation

2. 在layout文件夹下创建LayoutModule

import { NgModule } from '@angular/core';
import { MainLayoutComponent } from './main-layout/main-layout.component';
import { NavigationComponent } from './navigation/navigation.componennt';

@NgModule({
    imports: [],
    declarations: [
        MainLayoutComponent,
        NavigationComponent
    ],
    exports: []
})
export class LayoutModule { }

需要使用import将模块包含的组件都引入进来,然后在declarations中声明。

3.创建本模块中的组件以及相应的模板

  • MainLayoutComponent的代码如下:
import { Component } from '@angular/core';

@Component({
    selector: 'app-main-layout',
    templateUrl: './main-layout.component.html',
    styleUrls: []
})
export class MainLayoutComponent {}
  • main-layout.component.html代码很简单:
    <sa-navigation></sa-navigation> //组件使用同模块中的组件不需要import
  • NavigationComponent代码如下:
    import { Component } from '@angular/core';
    
    @Component({
        selector: 'sa-navigation',
        templateUrl: 'navigation.component.html',
        styleUrls: []
    })
    
    

export class NavigationComponent {}
```

  • navigation.component.html中,我们只写一句话
    Hello, world!

4.将模块引入AppModule

app.module.ts中代码如下:

//app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { AppComponent } from './app.component';
import { LayoutModule } from './layout/layout.module'; //这里需要import

@NgModule({
declarations: [
 AppComponent
],
imports: [
 BrowserModule,
 FormsModule,
 HttpModule,
 LayoutModule,       //这里需要imports
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
 ```
可以看出,分别在顶部的``import``和``@NgModule``中的``imports``数组中添加新加入的模块。

##5.使用模块中的组件
  使用模块中的组件,我们可以这样使用,在``app.component.html``中通过添加标签使用 ``<app-main-layout></app-main-layout>``
  我们虽然在AppModule中,引入了LayoutModule,但是你查看``layout.module.ts``文件,会发现``exports``数组是空的,也就是LayoutModule没有把这个组件暴露出来,这样AppModule中的组件不能找到``<app-main-layout>``所代表的组件的。
  所以一个模块中的组件想要被其他模块使用,需要在``exports``数组中暴露出去。在``layout.module.ts``中添加``exports``数组。

//layout.module.ts
@NgModule({
imports: [],
declarations: [
MainLayoutComponent,
NavigationComponent
],
exports: [
MainLayoutComponent //新添加的代码
]
})

**注意:**我们在``main-layout.component.html``中使用了``<sa-navigation>``,而没有导入,因为二者是处于同一个模块的,不需要额外导入。

##6.运行结果
  在项目目录下运行``npm start``,打开浏览器,就可以看到“Hello, world!”
##总结:
1. 模块A想要使用新创建的模块B,需要在A模块中导入B,并添加到A模块的``imports``数组中
2. A模块中的组件想要使用B模块中的组件b1,需要在B模块的``exports``数组中添加组件b1,即将其暴露出来。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.初步了解模块和组件 在上两篇文章中讲述了构建工程,这篇文章简单讲述一下Angular2中的模块和组件。 1.1...
    胡不归vac阅读 12,747评论 5 20
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,958评论 19 139
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,535评论 0 17
  • (二)脱缰的新生活 甄鱼原本不是一个性格太过外漏的女孩,外表矜持,其实内心火热,思维偶尔过于发散,文艺的讲,就是静...
    暮暮蜜阅读 486评论 3 3
  • 百日练100天读100本书,第26天。今天读的书名叫《赞美改变你》。讲的是:毒舌女玉婷最讨厌一天到晚嘴上抹蜜...
    梦境里的冰雪阅读 274评论 0 0