詳解Angular項(xiàng)目中共享模塊的實(shí)現(xiàn)
一、共享CommonModule
創(chuàng)建share Modele:ng g m share
import進(jìn)來(lái)所有需要共享的模塊都export出去,
暫時(shí)只有CommonModule,以后會(huì)有一些需要共享的組件。
import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; @NgModule({ imports: [ CommonModule ], exports:[ CommonModule ], declarations: [] }) export class SharedModule { }
在app Module中把core Module導(dǎo)入進(jìn)來(lái)。
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相關(guān)組件的導(dǎo)入導(dǎo)出單獨(dú)放在一個(gè)Moduel中,在ShareModule中導(dǎo)入導(dǎo)出即可。
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
確認(rèn)對(duì)話框不管是在刪除任務(wù)還是在刪除項(xiàng)目中都會(huì)用到,所以放在sharedModule中。
$ ng g c shared/confirm-dialog
也可以通過(guò)ng g c shared/confirm-dialog -it -is建一個(gè)內(nèi)聯(lián)的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項(xiàng)目中共享模塊的實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于Angular的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Angularjs 實(shí)現(xiàn)動(dòng)態(tài)添加控件功能
這篇文章主要介紹了Angularjs 實(shí)現(xiàn)動(dòng)態(tài)添加控件功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-05-05angular2 ng2-file-upload上傳示例代碼
這篇文章主要介紹了angular2 ng2-file-upload上傳示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08angular4模塊中給標(biāo)簽添加背景圖的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇angular4模塊中給標(biāo)簽添加背景圖的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09Angular基于Constructor?Parameter的依賴注入方式詳解
這篇文章主要為大家介紹了Angular基于Constructor?Parameter的依賴注入方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11