在Angular中,子模块默认无法直接访问父模块中的类。但是,可以通过在子模块中导入父模块并使用父模块中的类来解决这个问题。
以下是一个示例解决方法:
在父模块中创建一个类:
export class ParentClass {
public parentProperty: string = 'Parent Property Value';
}
在父模块中导出该类。
在子模块中导入父模块,并使用父模块中的类:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ParentClass } from 'path-to-parent-module';
@NgModule({
imports: [
CommonModule
],
declarations: [],
})
export class ChildModule {
constructor(private parentClass: ParentClass) {
console.log(this.parentClass.parentProperty); // 输出:Parent Property Value
}
}
在子模块的构造函数中注入父模块中的类,并通过该类的实例访问父模块中的属性或方法。
注意:在子模块中要确保正确导入父模块,并使用正确的路径引用父模块中的类。