Detailed explanation of the implementation of shared modules in Angular projects

Detailed explanation of the implementation of shared modules in Angular projects

1. Shared CommonModule

Create a share model: ng gm share

All modules that need to be shared are exported after importing.

For now, there is only CommonModule, and there will be some components that need to be shared in the future.

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

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

Import the core Module into the app 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 { }

2. Shared MaterialModule

For easy management, put the import and export of Material-related components in a separate Moduel, and import and export them in 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 { }

3. Shared ConfirmDialog

The confirmation dialog box is used whether you are deleting a task or a project, so it is placed in the sharedModule.

$ ng gc shared/confirm-dialog

You can also create an inline template and style using ng gc shared/confirm-dialog -it -is

<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)">OK</button>
    <button type="button" mat-button mat-dialog-close (click)="onClick(false)">Cancel</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);
  }
}

Then put the ConfirmDialogComponent component in 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 { }

If you use ConfirmDialog, refer to Deleting a Project.

The above is a detailed explanation of the implementation of shared modules in Angular projects. For more information about Angular, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of angular2 modules and shared modules
  • Detailed explanation of Angular structural directive modules and styles
  • Angular multi-module project build process
  • Specific usage of angular2 NgModel module
  • Example of Angular implementing preloading of delayed modules
  • Detailed explanation of lazy loading Angular modules using routing
  • A brief discussion on the lazy loading method of Angular2 modules

<<:  Summary of Nginx load balancing methods

>>:  Oracle deployment tutorial in Linux environment

Recommend

react-beautiful-dnd implements component drag and drop function

Table of contents 1. Installation 2.APi 3. react-...

JavaScript Advanced Programming: Variables and Scope

Table of contents 1. Original value and reference...

js realizes the magnifying glass effect of shopping website products

This article shares the specific code of js to ac...

A brief discussion on Mysql specified order sorting query

Recently, I have been working on a large-screen d...

Summary of three methods of lazy loading lazyLoad using native JS

Table of contents Preface Method 1: High contrast...

Nginx rush purchase current limiting configuration implementation analysis

Due to business needs, there are often rush purch...

MySQL sorting Chinese details and examples

Detailed explanation of MySQL sorting Chinese cha...

Detailed explanation of software configuration using docker-compose in linux

Preface This article will share some docker-compo...

8 JS reduce usage examples and reduce operation methods

reduce method is an array iteration method. Unlik...

Comparison of the use of form element attributes readonly and disabled

1) Scope of application: readonly:input[type="...

Nginx service 500: Internal Server Error one of the reasons

500 (Internal Server Error) The server encountere...

JavaScript implements an input box component

This article example shares the specific code for...

MySQL enables slow query (introduction to using EXPLAIN SQL statement)

Today, database operations are increasingly becom...

The whole process of realizing website internationalization using Vite2 and Vue3

Table of contents Preface Install vue-i18n Config...

CSS3 flexible box flex to achieve three-column layout

As the title says: The height is known, the width...