当サイトは、アフィリエイト広告を利用しています
Angularの@NgModuleのimportsとexportsの使って共有モジュールを作成して
他のモジュールで使用する方法をまとめる。
@NgModuleについては下記記事で紹介しています
importsはexportsされた他のmoduleを取り込んで、そのmodule内の
コンポーネントやディレクティブを使用することができるようになる。
サンプルを作って試してみる。
共有で使用するコンポーネントをまとめた
共有モジュール(shared.module.ts)を作る
共有モジュールとして扱うための共有コンポーネントを作る。
ng generate component shared/sharedButton
テンプレートファイルでボタンを作成する
<button>{{ title }}</button>
ボタンのタイトルを設定する
import { Component, OnInit } from '@angular/core';@Component({selector: 'app-shared-button',templateUrl: './shared-button.component.html',styleUrls: ['./shared-button.component.css'],})export class SharedButtonComponent implements OnInit {constructor() {}ngOnInit(): void {}title: string = '共有ボタン';}
下記のtsファイルも同時に作成されるが今回は使用しないため
割愛する。
ng generateでモジュールを作成する。
ng generate module shared
作成後は下記のようなフォルダ構成になる
-app|-shared|-shared-button| |-shared-button.component.css| |-shared-button.component.html| |-shared-button.component.spec.ts| |_shared-button.component.ts||_shared.module.ts
shared.module.ts内でSharedButtonComponentをexportsする
import { NgModule } from '@angular/core';import { CommonModule } from '@angular/common';import { SharedButtonComponent } from './shared-button/shared-button.component';@NgModule({declarations: [SharedButtonComponent],imports: [CommonModule],exports: [SharedButtonComponent],})export class SharedModule {}
SharedButtonComponentは他のmoduleで使用するためexportsする必要がある。
作成した共有モジュール(shared.module.ts)を
ルートモジュール(app.module.ts)でimportsする。
import { NgModule } from '@angular/core';import { BrowserModule } from '@angular/platform-browser';import { AppRoutingModule } from './app-routing.module';import { AppComponent } from './app.component';import { SharedModule } from './shared/shared.module';import { HelloComponent } from './hello/hello.component';@NgModule({declarations: [AppComponent, HelloComponent],imports: [BrowserModule, AppRoutingModule, SharedModule],providers: [],bootstrap: [AppComponent],})export class AppModule {}
共有モジュールを他のモジュール
を作成してその中で使ってみる。
sample-A,Bのフォルダとその直下に
sample-a,sample-bコンポーネントを作成する
フォルダ構成としては共有モジュールと同じように作る。
-app|-sample-A| |-sample-a| | |-sample-a.component.css| | |-sample-a.component.html| | |-sample-a.component.spec.ts| | |_sample-a.component.ts| || |_sample-a.module.ts|-sample-B| |-sample-b| | |-sample-b.component.css| | |-sample-b.component.html| | |-sample-b.component.spec.ts| | |_sample-b.component.ts| || |_sample-b.module.ts
ng generate component sample-A/sample-ang generate component sample-B/sample-b
※同時には実行できないので1個ずつ実行する
sample-a.component内で
共有コンポーネント< app-shared-button >を使用する
<p>sample-aコンポーネント</p><h3>共有コンポーネント↓</h3><app-shared-button></app-shared-button>
同じ容量でSampleBも作成する
ng generate module sample-Ang generate module sample-B
※同時には実行できないので1個ずつ実行する
sample-aのモジュールを編集して、exportsする。
import { NgModule } from '@angular/core';import { CommonModule } from '@angular/common';import { SampleAComponent } from './sample-a/sample-a.component';import { SharedModule } from '../shared/shared.module';@NgModule({declarations: [SampleAComponent],imports: [CommonModule, SharedModule],exports: [SampleAComponent],})export class SampleAModule {}
sample-bのモジュールを編集して、exportsする。
import { NgModule } from '@angular/core';import { CommonModule } from '@angular/common';import { SampleBComponent } from './sample-b/sample-b.component';import { SharedModule } from '../shared/shared.module';@NgModule({declarations: [SampleBComponent],imports: [CommonModule, SharedModule],exports: [SampleBComponent],})export class SampleBModule {}
作成したサンプルモジュールを取り込み、
ルーティング設定する。
SampleAModuleとSampleBModuleをimportsする。
import { NgModule } from '@angular/core';import { BrowserModule } from '@angular/platform-browser';import { AppRoutingModule } from './app-routing.module';import { AppComponent } from './app.component';import { SharedModule } from './shared/shared.module';import { HelloComponent } from './hello/hello.component';import { SampleAModule } from './sample-A/sample-a.module';import { SampleBModule } from './sample-B/sample-b.module';@NgModule({declarations: [AppComponent, HelloComponent],imports: [BrowserModule,AppRoutingModule,SharedModule,SampleAModule,SampleBModule,],providers: [],bootstrap: [AppComponent],})export class AppModule {}
SampleAComponentとSampleBComponentに
リンクで遷移するようにする。
<h1>Angular</h1><div><a routerLink="SampleAComponent">SampleA</a></div><div><a routerLink="SampleBComponent">SampleB</a></div><router-outlet></router-outlet>
SampleAComponentとSampleBComponentのルーティング設定
import { NgModule } from '@angular/core';import { RouterModule, Routes } from '@angular/router';import { SampleAComponent } from './sample-A/sample-a/sample-a.component';import { SampleBComponent } from './sample-B/sample-b/sample-b.component';const routes: Routes = [{path: 'SampleAComponent',component: SampleAComponent,// canDeactivate: [NavigationGuard],},{path: 'SampleBComponent',component: SampleBComponent,// canDeactivate: [NavigationGuard],},];@NgModule({imports: [RouterModule.forRoot(routes)],exports: [RouterModule],})export class AppRoutingModule {}
これでSampleAComponentとSampleBComponentでSharedButtonComponentを
共有することができる。
動作を確認したオンラインソースを下記に載せます。
下記を参考にさせて頂きました