Angular 2.0项目结构基本介绍

来源:互联网 发布:西班牙菜 知乎 编辑:程序博客网 时间:2024/06/04 19:16

    使用Angular-cli新建项目后,基本可以得到如下目录结构:


    1.根模块    

    其中app.module.ts是该项目的一个根模块,使用@NgModule修饰,表示该Class是一个模块,declarations表示该模块包含的视图类,imports表示该模块需要其他模块导出的类,provider是模块包含的servicebootstrap包含该模块启动时,需要启动的根组件,因此AppComponent是该项目的根组件。


    2.根组件

    不管是根组件还是普通组件,都是有@Component修饰,templateUrl表示该组件对应的html,styleUrls表示该模块对应的cssselector表示templateUrl在那个html元素开始显示。下面这个截图表示的就是,app.component.html对应的代码将在<app-root></app-root>中显示。


    如果是根组件的话,还需要在templateUrl对应的html代码中增加如下代码:

<router-outlet></router-outlet>
   这样其他组件对应的selector才能找到位置去显示。

    3.服务

    其中http.service.ts就是service,

@Injectable()export class HttpService {  constructor() { } }
    服务可以依赖注入,需要使用@Injectable修饰。当某个component需要使用某个service时,在component对应的construct中需要依赖注入对应的service,比如如下UserConponent需要使用HttpService,所以需要在construct中依赖注入。

    4.路由

    其中app.routing.ts就是对应的路由。其中path是对应的路径,conponent就是path对应的conponent。路由和service一样,当某个component需要使用路由时,需要在construct中声明



0 0
原创粉丝点击