我正在尝试使用我构建的自定义模块之一并将其放入另一个模块中
自定义模块:
我的-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 { }
我需要引入的另一个模块
my-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 {}
my-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 组件中。我究竟做错了什么?非常感谢!
更新
稍微更改我上面的代码以适合我的情况。它以 aMytestComponent
开头并this.myTestComponent
显示未定义。
1- 在 my-parent.component.html 添加 <test component selector> </ test component selector #test>
2- 在父 ts 文件中添加:
@ViewChild('test') test: MyTestComponent;
那么您将能够访问 MyTestComponent 中的所有方法和参数
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句