【问题标题】:VS code/Angular - Linter doesn't recognize components imported through shared moduleVS 代码/Angular - Linter 无法识别通过共享模块导入的组件
【发布时间】:2020-01-17 20:15:03
【问题描述】:

所以我的项目中有一个共享模块,其中我要导入我所有的 Angular 材料模块。我的代码构建并运行得很好。问题是,在我的 html 文件中,在 vs 代码中,我在每个角度材料组件标记下都有红色波浪线,并出现错误:

'mat-select' is not a known element:
1. If 'mat-select' is an Angular component, then verify that it is part of this module.
2. If 'mat-select' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.ng(0)

我已尝试重新启动 IDE。如果我将材料模块直接导入我的模块,而不是通过共享模块,该消息确实会消失。无论如何,这是我的代码:

shared.module.ts

...

@NgModule({
  imports: [
    CommonModule,
    ReactiveFormsModule,
    MatFormFieldModule,
    MatIconModule,
    MatInputModule,
    FlexLayoutModule,
    MatListModule,
    MatSelectModule,
    MatSidenavModule,
    MatToolbarModule
  ],
  exports:
  [
    ReactiveFormsModule,
    MatFormFieldModule,
    MatIconModule,
    MatInputModule,
    FlexLayoutModule,
    MatListModule,
    MatSelectModule,
    MatSidenavModule,
    MatToolbarModule
  ]
})
export class SharedModule { }

product.module.ts

...
@NgModule({
  declarations: [
    ProductComponent,
    ProductFormComponent,
    AddProductToStoreFormComponent,
    StorePickerComponent,
    StorePickerRootComponent,
    SizeFormComponent,
    CategoryPickerRootComponent,
    CategoryPickerComponent

  ],
  imports: [
    CommonModule,
    ProductRoutingModule,
    SharedModule
  ]
})
export class ProductModule { }

感谢收看。

【问题讨论】:

    标签: angular visual-studio-code angular-module


    【解决方案1】:

    为什么要在导出部分导出其他模块?导出允许其他模块访问声明您的模块的组件/管道。不是模块。

    所以在product.module.ts中,导入MatFormFieldModule

    【讨论】:

    • 我正在从我的“共享”模块中导出模块,这样我的所有其他模块都可以导入一个模块并获取所有常见的导入。这样我就不必在不同的模块中多次导入 MatFormFieldModule 了。 angular.io/guide/sharing-ngmodules
    【解决方案2】:

    我想通了。我还安装了 nativescript 并使用 @src 路径导入共享模块,正如它所建议的那样:

    import { SharedModule } from '@src/app/shared/shared.module';
    

    一旦我恢复使用相对路径:

    import { SharedModule } from '../../shared/shared.module';
    

    一切开始正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-29
      • 2023-04-08
      • 2020-07-23
      • 2021-01-18
      • 2017-02-15
      相关资源
      最近更新 更多