详解Angular项目中共享模块的实现

编程学习 2021-07-04 13:23www.dzhlxh.cn编程入门
本文主要介绍了Angular的共享模块的实现,对此感兴趣的同学,可以实验一下

一、共享CommonModule

创建share Modele:ng g m share

import进来所有需要共享的模块都export出去,

暂时只有CommonModule,以后会有一些需要共享的组件。

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

@NgModule({
  imports: [
    CommonModule
  ],
  exports:[
    CommonModule
  ],
  declarations: []
})
export class SharedModule { }

在app Module中把core Module导入进来。

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

import { AppComponent } from './app.component';
import {CoreModule} from './core/core.module';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    CoreModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

二、共享MaterialModule

为了方便管理,把Material相关组件的导入导出单独放在一个Moduel中,在ShareModule中导入导出即可。

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MatToolbarModule, MatSidenavModule, MatButtonModule, MatCardModule, MatInputModule, MatListModule, MatSlideToggleModule, MatGridListModule, MatDialogModule, MatAutocompleteModule, MatMenuModule, MatCheckboxModule, MatTooltipModule, MatDatepickerModule, MatRadioModule, MatNativeDateModule, MatSelectModule } from '@angular/material';
import { MatIconModule } from '@angular/material';

const module=[
  MatSidenavModule,
  MatIconModule,
  MatToolbarModule,
  MatIconModule,
  MatButtonModule,
  MatCardModule,
  MatInputModule,
  MatListModule,
  MatSlideToggleModule,
  MatGridListModule,
  MatDialogModule,
  MatAutocompleteModule,
  MatMenuModule,
  MatCheckboxModule,
  MatTooltipModule,
  MatDatepickerModule,
  MatRadioModule,
  MatNativeDateModule,
  MatSelectModule
];

@NgModule({
  declarations: [],
  imports: [
    module
  ],
  exports:[
    module
  ]
})
export class MaterialModule { }
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MaterialModule } from '../material/material.module';
import { ConfirmDialogComponent } from './confirm-dialog/confirm-dialog.component';

@NgModule({
  imports: [
    CommonModule,
    MaterialModule
  ],
  exports:[
    CommonModule,
    MaterialModule
  ],
  declarations: [ConfirmDialogComponent]
})
export class SharedModule { }

三、共享ConfirmDialog

确认对话框不管是在删除任务还是在删除项目中都会用到,所以放在sharedModule中。

$ ng g c shared/confirm-dialog

也可以通过ng g c shared/confirm-dialog -it -is建一个内联的template和style

<form>
  <h2 md-dialog-title>{{title}}</h2>
  <div mat-dialog-content>
    {{content}}
  </div>
  <div mat-dialog-actions>
    <button type="button" mat-raised-button color="primary" (click)="onClick(true)">确定</button>
    <button type="button" mat-button mat-dialog-close  (click)="onClick(false)">取消</button>
  </div>
</form>
import { Component, OnInit, Inject } from "@angular/core";
import { MatDialogRef } from "@angular/material";
import { MAT_DIALOG_DATA } from "@angular/material";

@Component({
  selector: "app-confirm-dialog",
  templateUrl: "./confirm-dialog.component.html",
  styleUrls: ["./confirm-dialog.component.scss"]
})
export class ConfirmDialogComponent implements OnInit {
  title = "";
  content = "";
  constructor(
    private dialogRef: MatDialogRef<ConfirmDialogComponent>,
    @Inject(MAT_DIALOG_DATA) private data
  ) { }

  ngOnInit() {
    this.title = this.data.title;
    this.content = this.data.content;
  }
  onClick(result: boolean) {
    this.dialogRef.close(result);
  }
}

然后把ConfirmDialogComponent组件放在sharedModule中。

import { NgModule } from "@angular/core";
import { CommonModule } from "@angular/common";
import { MaterialModule } from "../material/material.module";
import { ConfirmDialogComponent } from "./confirm-dialog/confirm-dialog.component";

@NgModule({
  imports: [CommonModule, MaterialModule],
  exports: [CommonModule, MaterialModule],
  declarations: [ConfirmDialogComponent],
  entryComponents: [ConfirmDialogComponent]
})
export class SharedModule { }

如果使用ConfirmDialog,可参考删除project。

以上就是详解Angular项目中共享模块的实现的详细内容,更多关于Angular的资料请关注狼蚁SEO其它相关文章!

Copyright © 2016-2025 www.dzhlxh.cn 金源码 版权所有 Power by

网站模板下载|网络推广|微博营销|seo优化|视频营销|网络营销|微信营销|网站建设|织梦模板|小程序模板