Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Angular 2中通信组件_Angular - Fatal编程技术网

如何在Angular 2中通信组件

如何在Angular 2中通信组件,angular,Angular,我正在使用Angular 2(在其最新版本中)构建一个应用程序,我希望在组件之间进行通信 这是我的工作区: src app navbar navbar.component.ts footer footer.component.ts section dashboard dashboard.component.ts s

我正在使用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
我想从dashboard.component.ts调用footer.component.ts中的方法

这是我的代码:

footer.component.ts

export class FooterComponent implements OnInit {

  ngOnInit() {

  }

  walking(){
    console.log("Method of called walking");
  }

}
export class DashboardComponent implements OnInit {

  ngOnInit() {

  }

  callWalking(){
    console.log("Call to walking method");
  }

}
@NgModule({
  declarations: [
    AppComponent,
    SectionComponent,
    SidebarComponent,
    NavbarComponent,
    FooterComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    SectionModule,
    RouterModule.forRoot([])
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
@NgModule({
  imports: [
    CommonModule,
    RouterModule.forRoot(
      [
        { path: 'dashboard', component: DashboardComponent },
        { path: '', redirectTo: 'dashboard', pathMatch: 'full' },
        { path: '**', component: DashboardComponent }
      ]
    )
  ],
  declarations: [DashboardComponent]
})
export class SectionModule { }
仪表板.component.ts

export class FooterComponent implements OnInit {

  ngOnInit() {

  }

  walking(){
    console.log("Method of called walking");
  }

}
export class DashboardComponent implements OnInit {

  ngOnInit() {

  }

  callWalking(){
    console.log("Call to walking method");
  }

}
@NgModule({
  declarations: [
    AppComponent,
    SectionComponent,
    SidebarComponent,
    NavbarComponent,
    FooterComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    SectionModule,
    RouterModule.forRoot([])
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
@NgModule({
  imports: [
    CommonModule,
    RouterModule.forRoot(
      [
        { path: 'dashboard', component: DashboardComponent },
        { path: '', redirectTo: 'dashboard', pathMatch: 'full' },
        { path: '**', component: DashboardComponent }
      ]
    )
  ],
  declarations: [DashboardComponent]
})
export class SectionModule { }
应用程序模块.ts

export class FooterComponent implements OnInit {

  ngOnInit() {

  }

  walking(){
    console.log("Method of called walking");
  }

}
export class DashboardComponent implements OnInit {

  ngOnInit() {

  }

  callWalking(){
    console.log("Call to walking method");
  }

}
@NgModule({
  declarations: [
    AppComponent,
    SectionComponent,
    SidebarComponent,
    NavbarComponent,
    FooterComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    SectionModule,
    RouterModule.forRoot([])
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
@NgModule({
  imports: [
    CommonModule,
    RouterModule.forRoot(
      [
        { path: 'dashboard', component: DashboardComponent },
        { path: '', redirectTo: 'dashboard', pathMatch: 'full' },
        { path: '**', component: DashboardComponent }
      ]
    )
  ],
  declarations: [DashboardComponent]
})
export class SectionModule { }
app.component.html

<app-navbar></app-navbar>
<app-sidebar></app-sidebar>
<app-section></app-section>
<app-footer></app-footer>
<router-outlet></router-outlet>
section.component.html

<app-navbar></app-navbar>
<app-sidebar></app-sidebar>
<app-section></app-section>
<app-footer></app-footer>
<router-outlet></router-outlet>

我确实想从component dashboard.component.ts中调用组件“footer.component.ts”的“walk()”方法

export class FooterComponent implements OnInit {

  ngOnInit() {

  }

  walking(){
    console.log("Method of called walking");
  }

}
export class DashboardComponent implements OnInit {

  ngOnInit() {

  }

  callWalking(){
    console.log("Call to walking method");
  }

}
@NgModule({
  declarations: [
    AppComponent,
    SectionComponent,
    SidebarComponent,
    NavbarComponent,
    FooterComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    SectionModule,
    RouterModule.forRoot([])
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
@NgModule({
  imports: [
    CommonModule,
    RouterModule.forRoot(
      [
        { path: 'dashboard', component: DashboardComponent },
        { path: '', redirectTo: 'dashboard', pathMatch: 'full' },
        { path: '**', component: DashboardComponent }
      ]
    )
  ],
  declarations: [DashboardComponent]
})
export class SectionModule { }
如何从panel component method>callWalking()调用“footer component>walk()”方法?

在JavaAndroid中,我可以使用静态方法或为 交流的活动或片段,角度我不知道 我能做什么


一个选项是在组件上添加@input和@output属性。然后,可以使用@output属性将数据从组件传递到其父组件,父组件可以使用@input属性将信息传递到其他子组件之一


第二种选择是构建角度服务来管理通信。任何组件都可以调用服务中的方法,其他组件可以绑定到服务属性。

构建角度服务以管理通信。

1。创建服务

import { Injectable, Output,EventEmitter } from '@angular/core';

@Injectable()
export class TestService {



@Output() event$:EventEmitter<boolean>=new EventEmitter();

  constructor() { 

  }

  setEventEmitter(enable:boolean) {
      this.event$.next(enable);
  }

  getEventEmitter() {
     return this.event$;
  }  

}
....

providers: [TestService],

bootstrap: [AppComponent]

....
3。发射事件

constructor(private ls: TestService) { }

ngOnInit() {
      this.ls.setEventEmitter(true);  
}
constructor(private ls: TestService) { }

ngOnInit() {
  this.ls.getEventEmitter().subscribe(e => {
        if(e != null) {
            console.log("Code here");
        }
    });  
}
4。接收事件

constructor(private ls: TestService) { }

ngOnInit() {
      this.ls.setEventEmitter(true);  
}
constructor(private ls: TestService) { }

ngOnInit() {
  this.ls.getEventEmitter().subscribe(e => {
        if(e != null) {
            console.log("Code here");
        }
    });  
}

步行到底是做什么的?如果它只是一个工具,我会把它分解成它自己的类,然后把它拉到任何你需要的地方。如果确实需要在两个组件之间传递信息,也可以使用输入绑定:@DanWeber在不同组件之间传递数据有效吗?文档中说:通过传入链接将父对象的数据传递给子对象,我的组件不是父对象和子对象。或者至少我不知道它们是否是,我想不是:(.它们都是子级(嵌套组件)关于应用程序组件。看一看这个帖子:非常感谢,你的回答帮助我找到了一个好的起点。我决定使用选项2。在发布我对这个问题的回答后,我想知道这是否是最好的方法,可能还有更好的方法。我对这个例子中eventemitter的用途很好奇。我发现我可以简单地访问服务的属性,如果它们被绑定,它们将相应地更改…无需发出事件。但我认为我可能缺少一个用例?(我的示例中没有eventEmitter:)