簡體   English   中英

如何在Angular 4中導入自定義模塊?

[英]How to import custom modules in Angular 4?

我正在嘗試使用已構建的自定義模塊之一並將其放在另一個模塊中

定制模塊:

我-test.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { MyTestComponent } from './my-test.component';

@NgModule({
  declarations: [
    MyTestComponent    
  ],
  imports: [
    CommonModule,    
  ],  
  exports: [ MyTestComponent]
})
export class MyTestModule { }

我需要插入的另一個模塊

我-parent.module.ts

import {CommonModule} from '@angular/common';
import {NgModule} from '@angular/core';

import { MyParentComponent } from './my-parent.component';
import { MyTestModule } from '../my-test/my-test.module';

@NgModule({
    'imports': [
        CommonModule,
        MyTestModule        
    ],
    'declarations': [
        MyParentComponent
    ],    
    'exports': [
        MyParentComponent
    ]   
})

export class MyParentModule {}

我-parent.component.ts

import { Component, Inject, Injectable, OnInit  } from '@angular/core';
import { MyTestComponent } from '../my-test/my-test.component'

@Component({
    'selector': 'my-parent',
    'template': `
       test
    `
})

@Injectable()

export class MyParentComponent implements OnInit {    
    public myTestComponent: MyTestComponent;
    constructor() {}

    ngOnInit() {
       console.log(this.myTestComponent)  <----show undefined here.
    }    
}

我不確定如何將my-test組件導入my-parent組件。 我究竟做錯了什么? 非常感謝!

更新

略微更改上面的代碼以適合我的情況。 它以MytestComponent開頭,並且this.myTestComponent顯示未定義。

1-在my-parent.component.html中添加<test component selector> </ test component selector #test>

2-在父ts文件中添加:

@ViewChild('test') test: MyTestComponent;

那么您將能夠訪問MyTestComponent中的所有方法和參數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM