Angular で @ViewChild と @ContentChild デコレータを使用する
Angular コンポーネントを別のモジュールから使用する方法
imports と exports を使用する
これは最も一般的な方法です。コンポーネントを使用したいモジュールで、以下の手順を行います。
- コンポーネントが定義されているモジュールを
imports
します。 - 使用したいコンポーネントを
exports
からインポートします。 - コンポーネントをテンプレートファイルで使用します。
例:
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule
],
bootstrap: [AppComponent]
})
export class AppModule { }
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { UserComponent } from './user.component';
@NgModule({
declarations: [
UserComponent
],
imports: [
CommonModule
],
exports: [
UserComponent
]
})
export class UserModule { }
home.component.html
<user-component></user-component>
この方法を使う場合は、以下の点に注意する必要があります。
- コンポーネントは、
exports
で明示的に公開されている必要があります。 - コンポーネントとその依存関係は、使用しているモジュールに存在する必要があります。
forRoot を使用する
サービスなど、シングルトンとして使用されるコンポーネントの場合は、forRoot
メソッドを使用することができます。
user.service.ts
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class UserService {
constructor() { }
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { UserModule } from './user/user.module';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
UserModule.forRoot()
],
bootstrap: [AppComponent]
})
export class AppModule { }
- コンポーネントは、
providedIn
プロパティを'root'
に設定する必要があります。 - コンポーネントは、ルートモジュールでインポートする必要があります。
entryComponents を使用する
ダイナミックコンポーネントなど、動的に作成されるコンポーネントの場合は、entryComponents
プロパティを使用することができます。
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'user-component',
templateUrl: './user.component.html',
styleUrls: ['./user.component.css']
})
export class UserComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { UserModule } from './user/user.module';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
UserModule
],
bootstrap: [AppComponent],
entryComponents: [
UserComponent
]
})
export class AppModule { }
- コンポーネントは、
entryComponents
プロパティで明示的に登録する必要があります。
Angular コンポーネントを別のモジュールから使用するには、いくつかの方法があります。それぞれの方法にはメリットとデメリットがあるので、状況に応じて適切な方法を選択する必要があります。
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { UserModule } from './user/user.module';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
UserModule
],
bootstrap: [AppComponent]
})
export class AppModule { }
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { UserComponent } from './user.component';
@NgModule({
declarations: [
UserComponent
],
imports: [
CommonModule
],
exports: [
UserComponent
]
})
export class UserModule { }
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'user-component',
templateUrl: './user.component.html',
styleUrls: ['./user.component.css']
})
export class UserComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
<user-component></user-component>
このサンプルコードでは、UserComponent
を UserModule
で定義し、AppModule
でインポートして使用しています。
forRoot メソッドを使用する場合は、UserService
を以下のように変更します。
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class UserService {
constructor() { }
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { UserModule } from './user/user.module';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
UserModule.forRoot()
],
bootstrap: [AppComponent]
})
export class AppModule { }
entryComponents プロパティを使用する場合は、UserComponent
を以下のように変更します。
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'user-component',
templateUrl: './user.component.html',
styleUrls: ['./user.component.css']
})
export class UserComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { UserModule } from './user/user.module';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
UserModule
],
bootstrap: [AppComponent],
entryComponents: [
UserComponent
]
})
export class AppModule { }
上記以外にも、コンポーネントを別のモジュールから使用する方法があります。詳細は、Angular の公式ドキュメントを参照してください。
Angular コンポーネントを別のモジュールから使用するその他の方法
ng-content
は、コンポーネントのテンプレート内に他のコンポーネントを投影するための方法です。
<div>
<ng-content></ng-content>
</div>
<h1>Hello World</h1>
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { ParentComponent } from './parent/parent.component';
import { ChildComponent } from './child/child.component';
@NgModule({
declarations: [
AppComponent,
ParentComponent,
ChildComponent
],
imports: [
BrowserModule
],
bootstrap: [AppComponent]
})
export class AppModule { }
- 親コンポーネントは、
ng-content
ディレクティブを使用する必要があります。 - 子コンポーネントは、親コンポーネントのテンプレート内に投影される必要があります。
@ViewChild
と @ContentChild
は、コンポーネントのテンプレート内またはコンポーネントの内容から子コンポーネントへの参照を取得するためのデコレータです。
import { Component, OnInit, ViewChild, ContentChild } from '@angular/core';
import { ChildComponent } from './child.component';
@Component({
selector: 'parent-component',
templateUrl: './parent.component.html',
styleUrls: ['./parent.component.css']
})
export class ParentComponent implements OnInit {
@ViewChild(ChildComponent) childComponent: ChildComponent;
constructor() { }
ngOnInit() {
}
}
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'child-component',
templateUrl: './child.component.html',
styleUrls: ['./child.component.css']
})
export class ChildComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { ParentComponent } from './parent/parent.component';
import { ChildComponent } from './child/child.component';
@NgModule({
declarations: [
AppComponent,
ParentComponent,
ChildComponent
],
imports: [
BrowserModule
],
bootstrap: [AppComponent]
})
export class AppModule { }
- 親コンポーネントは、
@ViewChild
デコレータを使用して子コンポーネントへの参照を取得する必要があります。
サービスを使用する
コンポーネント間でデータを共有する必要がある場合は、サービスを使用することができます。
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class UserService {
constructor() { }
getUser() {
return {
name: 'John Doe',
email: '[email protected]'
};
}
}
import { Component, OnInit } from '@angular/core';
import { UserService } from './user.service';
@Component({
selector: 'parent-component',
templateUrl: './parent.component.html',
styleUrls: ['./parent.component.css']
})
export class ParentComponent implements OnInit {
user: any;
constructor(private userService: UserService) { }
ngOnInit() {
this.user = this.userService.getUser();
}
}
angular typescript angular-module