【问题标题】:How to access a component from different module in angular 2如何在角度 2 中从不同模块访问组件
【发布时间】:2016-11-02 12:43:41
【问题描述】:

我是 Angular 2 的新手,所以如果这个问题对你来说听起来微不足道,请原谅。我正在 Angular 2 中创建一个功能模块,我将从该模块中导出所有组件。主模块可以导入它并将该模块添加到导入列表中。这样,主模块中的所有“模板”都可以访问功能模块的组件。

但我想要的是:在我的主要模块组件之一中,我想将功能模块的组件称为 ViewChild。

【问题讨论】:

    标签: angular angular2-components angular2-modules


    【解决方案1】:

    您需要使用 TypeScript 导入来导入组件类,例如

    import {MyComponent} from './my.component'
    

    那么你可以在@ViewChild()使用它

    @ViewChild(MyComponent) myComponent:MyComponent;
    

    【讨论】:

    • 是否有权从不同的模块访问内部文件?如果它在同一个模块中,这似乎很好。如果我这样走,为什么我需要把它放在功能模块的导入列表中。
    • 你可能是对的。但是您根本没有使用模块功能。您只是绕过模块。正如我之前写的,如果组件不在功能模块的导出列表中,那会起作用吗?我想,它会的。
    • 这两件事没有关系。您需要 @NgModule()s、declarations: []imports: []exports: [] 来在模块模板中使用组件、指令和管道以及使用路由器进行延迟加载,但对于 @ViewChild(),您只需要 TypeScript导入以使用MyComponent 明确您所指的类。这后面的部分与@NgModule() 没有任何关系。
    • 这听起来合乎逻辑。这是来自 Angular 站点的文本:“导入 BrowserModule 使其所有公共组件、指令和管道对 AppModule 中的组件模板可见”。导入的对象仅适用于模板。
    【解决方案2】:

    @ViewChild 可以与本地 id 一起使用(以 # 为前缀),您不必导入定义类。当然,您不能将变量 comp 键入为 MyComponent

    在模板中:

    <my-component #myComponent></my-component>
    

    在javascript中:

    @ViewChild('myComponent') comp: any;
    

    (注意引号)

    或者
    您可以从功能模块重新导出组件

    // my.module.ts
    
    import {MyComponent} from './my.component'
    
    @NgModule({
      ...
    })
    export class MyModule {}
    export {MyComponent} 
    

    然后在消费组件中(您要使用@ViewChild)

    import {MyComponent} from '../common/my.module'
    

    现在 Typescript 有一个对该类的工作引用,但只需要引用功能模块,而不是单个组件。

    【讨论】:

      【解决方案3】:

      ViewChild 是一个装饰器,用于查询模板以获取从功能模块导入的子级。 如果您的模板是:

      <div>
        <child></child>
      </div>
      

      通过使用@ViewChild,您可以获得您的子组件参考

      【讨论】:

      • 很抱歉,我无法得到它。能否请您详细说明
      【解决方案4】:

      您可以为此使用 service 和 EventEmitter。

      import { Injectable, EventEmitter } from '@angular/core';
      
      
          @Injectable()
          export class MyService  {
      
              resultIdFound: EventEmitter<any> = new EventEmitter<any>();
      
              resultFound(resultId: string) {
                  this.resultIdFound.emit(resultId)
              }
          }
      

      源组件是:

      import { Component, EventEmitter } from '@angular/core';
      import { MyService } from './myservice';
      
          @Component({
          //..
          })
          export class SourceComponent implements OnInit {
              constructor(
                  private router: Router,
                  private myService: MyService) { }
      
              onResultFound(resultId: string): void {
      
                  this.myService.roomSeached(this.selectedRoom.id)
              }
          }
      

      目标组件是:

      import { Component, EventEmitter,NgModule } from '@angular/core';
      import { MyService } from './myService';
      
          @Component({
          //...
          })
          export class TargetComponent implements OnInit {
              constructor( 
                  private myService: MyService
              ) {
                 this.myService.resultIdFound.subscribe((result: any) => {
                       console.log(result)
                  });
      
              }    
              //....    
          }
      

      【讨论】:

      • 感谢您的回答!但这是一种使不同组件之间的通信成为可能的工作。你的意思是一个模块中的一个组件不能使用 ViewChild 访问不同模块中的另一个组件吗?
      【解决方案5】:

      请考虑创建一个共享模块(功能),该模块将向两个模块提供该组件。使用共享模块构建应用程序请参阅官方documents

      【讨论】:

      • 链接已过期!说“找不到页面!” !
      • 链接已启动并正在运行。它已经移动了一段时间
      • 不!它仍然是“找不到页面!” !
      猜你喜欢
      • 2019-11-08
      • 2015-08-03
      • 1970-01-01
      • 2020-07-06
      • 2017-10-03
      • 1970-01-01
      • 2018-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多