angular 自定义模块

本文详细介绍了如何在Angular项目中实现模块化与组件化的开发流程,包括创建项目、自定义模块、组件的导出及使用等关键步骤,帮助开发者更有效地管理和组织项目。

模块化,组件化,更加方便项目的管理,开发

1 创建新的项目.

2 创建自定义模块

命令:ng g module [pah/modulename]

3 同理,可以在自定义模块下创建模块,包括根模块以及其他普通模块

命令:ng g component [path/componentname]

4 在user.module.ts中exports组件,外部模块才能使用

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HomeComponent } from './components/home/home.component';
import { UserComponent } from './user.component';




@NgModule({
  declarations: [HomeComponent, UserComponent],
  imports: [
    CommonModule
  ],
  // 自定义模块的组件需要暴露出去才能使用
  exports:[UserComponent]
})

export class UserModule { }

  如果没有exports组件:

 

 导出之后:

 

5 在app.module.ts中导入自定义模块

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

// 和上面的核心模块样,使用自定义模块需要先import,同时放入@NgModule()中
import { UserModule } from "./modules/user/user.module";

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    // 此处需要添加
    UserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

6 例如在app.components.html中直接使用

<app-user></app-user>

效果

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值