Angular 2(最新バージョン)でアプリケーションを構築していますが、コンポーネント間で通信したいと思います。私はdashboard.component.tsからfooter.component.tsでメソッドを呼び出したいAngular 2でコンポーネントを通信する方法
src
app
navbar
navbar.component.ts
footer
footer.component.ts
section
dashboard
dashboard.component.ts
section.component.html
section.component.ts
section.module.ts
sidebar
sidebar.component.ts
app.component.html
app.module.ts
assets
は、これは私のワークスペースです。
これは私のコードです:
footer.component.ts
export class FooterComponent implements OnInit {
ngOnInit() {
}
walking(){
console.log("Method of called walking");
}
}
dashboard.component.ts
export class DashboardComponent implements OnInit {
ngOnInit() {
}
callWalking(){
console.log("Call to walking method");
}
}
app.module.ts
@NgModule({
declarations: [
AppComponent,
SectionComponent,
SidebarComponent,
NavbarComponent,
FooterComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
SectionModule,
RouterModule.forRoot([])
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
app.component.html
<app-navbar></app-navbar>
<app-sidebar></app-sidebar>
<app-section></app-section>
<app-footer></app-footer>
section.module.ts
@NgModule({
imports: [
CommonModule,
RouterModule.forRoot(
[
{ path: 'dashboard', component: DashboardComponent },
{ path: '', redirectTo: 'dashboard', pathMatch: 'full' },
{ path: '**', component: DashboardComponent }
]
)
],
declarations: [DashboardComponent]
})
export class SectionModule { }
section.component.html
<router-outlet></router-outlet>
私は文字通り「フッタコンポーネント>ウォーク()」メソッドを呼び出すためにどのようにコンポーネントdashboard.component.tsから
を成分「footer.component.ts」の「歩く()」メソッドを呼び出したいですパネルコンポーネントメソッドから> callWalking()? Javaのアンドロイドで
私は角に私は
callWalkingは実際に何をしていますか?それが単なるツールであれば、私はそれを自分のクラスに分解し、それが必要などこにでも引き込むことができます。実際に2つのコンポーネント間で情報を渡す必要がある場合は、入力バインディングを使用することもできます。https://angular.io/guide/component-interaction#!#bidirectional-service –
@DanWeber異なるコンポーネント間でデータを渡すことはできますか?ドキュメントには次のように書かれています:親のデータを入ってくるリンクを持つ子に渡し、私のコンポーネントは親と子ではありません。または、少なくとも私は彼らがそうであるかどうかわからない:(。 –
彼らはすべて、アプリケーションコンポーネントのすべての子(入れ子コンポーネント)です。 – DeborahK