当サイトは、アフィリエイト広告を利用しています

【Angular】共有componentを作る~importsとexportsの使い方~

作成日:2022月11月30日
更新日:2024年01月28日

Angularの@NgModuleのimportsとexportsの使って共有モジュールを作成して
他のモジュールで使用する方法をまとめる。 @NgModuleについては下記記事で紹介しています

importsはexportsされた他のmoduleを取り込んで、そのmodule内の
コンポーネントやディレクティブを使用することができるようになる。

サンプルを作って試してみる。

共有モジュールの作成

共有で使用するコンポーネントをまとめた
共有モジュール(shared.module.ts)を作る

共有コンポーネント作成

共有モジュールとして扱うための共有コンポーネントを作る。

GitBash
ng generate component shared/sharedButton
  • sharedフォルダ配下にsharedButtonコンポーネントが作成される。

shared-button.component.html

テンプレートファイルでボタンを作成する

shared-button.component.html
<button>{{ title }}</button>

shared-button.component.ts

ボタンのタイトルを設定する

shared-button.component.ts
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ファイルも同時に作成されるが今回は使用しないため
割愛する。

  • shared-button.component.css
  • shared-button.component.spec.ts

ng generateでモジュールを作成する。

GitBash
ng generate module shared
  • sharedフォルダ直下にshared.module.tsを作成する

作成後は下記のようなフォルダ構成になる

フォルダ構成
-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の編集

shared.module.ts内でSharedButtonComponentをexportsする

shared.module.ts
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する必要がある。

app.module.tsの編集

作成した共有モジュール(shared.module.ts)を
ルートモジュール(app.module.ts)でimportsする。

app.module.ts
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

を作成してその中で使ってみる。

コンポーネントとモジュールを作成する

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

コンポーネント作成

GitBash
ng generate component sample-A/sample-a
ng generate component sample-B/sample-b

※同時には実行できないので1個ずつ実行する

sample-a.component.htmlの編集

sample-a.component内で
共有コンポーネント< app-shared-button >を使用する

sample-a.component.html
<p>sample-aコンポーネント</p>
<h3>共有コンポーネント↓</h3>
<app-shared-button></app-shared-button>

同じ容量でSampleBも作成する

モジュールを作成

GitBash
ng generate module sample-A
ng generate module sample-B

※同時には実行できないので1個ずつ実行する

sample-a.module.tsの編集

sample-aのモジュールを編集して、exportsする。

sample-a.module.ts
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 {}
  • 作成したSampleAComponentを取り込む
  • SharedModuleをimportsに設定する。
  • SampleAComponent自体をexportsする。

sample-b.module.tsの編集

sample-bのモジュールを編集して、exportsする。

sample-b.module.ts
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 {}

ルートモジュールの編集

作成したサンプルモジュールを取り込み、
ルーティング設定する。

app.module.tsの編集

SampleAModuleとSampleBModuleをimportsする。

app.module.ts
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 {}

app.component.htmlの編集

SampleAComponentとSampleBComponentに
リンクで遷移するようにする。

app.component.html
<h1>Angular</h1>
<div>
<a routerLink="SampleAComponent">SampleA</a>
</div>
<div>
<a routerLink="SampleBComponent">SampleB</a>
</div>
<router-outlet></router-outlet>
  • < router-outlet >の部分がSampleAComponentもしくはSampleBComponentに
    置き換わる

app-routing.module.tsの編集

SampleAComponentとSampleBComponentのルーティング設定

ts
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を
共有することができる。

codesandboxで動作確認

動作を確認したオンラインソースを下記に載せます。

angular_sample (imports_exports)

参考

下記を参考にさせて頂きました

新着記事

タグ別一覧
top