在Angular 8之前,我们使用import { NgModule } from '@angular/core';
语句来导入NgModule模块。但是在Angular 8中,NgModule模块的导入语法有所改变。
在Angular 8中,我们使用import { NgModule } from '@angular/core';
语句导入NgModule模块时,还需要添加@angular/common
模块的导入语句。具体代码示例如下:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
@NgModule({
imports: [
CommonModule
],
declarations: [
// 组件声明
],
providers: [
// 服务提供者
]
})
export class MyModule { }
在上面的代码中,我们通过import { CommonModule } from '@angular/common';
语句导入了CommonModule
模块。然后在NgModule的imports
数组中添加了CommonModule
模块。
这是因为在Angular 8中,CommonModule
被分成了独立的模块,而不再是@angular/core
的一部分。因此,我们需要单独导入CommonModule
模块来使用其中的指令和管道。
需要注意的是,CommonModule
是一个常用的模块,它提供了一些常用的指令和管道,如*ngIf
、*ngFor
等。如果你的模块中使用到了这些指令和管道,就需要导入CommonModule
模块。
除了CommonModule
模块之外,还有其他模块也需要单独导入,具体取决于你的代码中使用了哪些模块。在导入模块时,可以根据需要自行添加所需的模块。
希望对你有所帮助!