Javascript 如何在Angular 4中导入自定义模块?
我正在尝试使用我构建的一个自定义模块,并将其放在另一个模块中 自定义模块: my-test.module.tsJavascript 如何在Angular 4中导入自定义模块?,javascript,angular,module,Javascript,Angular,Module,我正在尝试使用我构建的一个自定义模块,并将其放在另一个模块中 自定义模块: my-test.module.ts import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { MyTestComponent } from './my-test.component'; @NgModule({ declarations: [ MyTestCompone
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 { }
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.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 { }
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.
}
}
从'@angular/core'导入{Component,Inject,Injectable,OnInit};
从“../my test/my test.component”导入{MyTestComponent}
@组成部分({
“选择器”:“我的父母”,
“模板”:`
测试
`
})
@可注射()
导出类MyParentComponent实现OnInit{
公共myTestComponent:myTestComponent;
构造函数(){}
恩戈尼尼特(){
console.log(this.myTestComponent)1-在my-parent.component.html中添加
2-在父ts文件中添加:
@ViewChild('test') test: MyTestComponent;
然后,您将能够访问MyTestComponent中的所有方法和参数您想在my-parent.module.ts中使用my-test.module.ts吗?@mohammad是的,这是我的意图我使用了与您相同的结构,但组件应该具有初始值的唯一问题是当我添加={}时,{}显示的是不可赋值错误。另外,为什么要为组件赋值?+1在JavaScript和类型脚本中,参数默认为未定义。但是,在某些情况下,设置不同的默认值可能会很有用。这是默认参数可以提供帮助的地方。因此,对于编译器公共myTest,它看起来是这样的:MyTestComponent='undefined';如果您如果我的答案对您来说是稳定的,请通过将答案标记为正确答案来结束问题。我仍然有问题。我无法将{}分配给组件。它说{}不可分配以键入MyTestComponent。让我们了解您想要做什么,因为导出无法像那样工作。在我们解决问题之前,我将帮助您