当我尝试将 bootstrap 添加到我的 Angular 项目时,出现此错误:
MainComponent类是一个独立的组件,不能被 在 @NgModule.bootstrap 数组中使用。使用 bootstrap 应用程序 代替引导函数
完全在 app.module 文件中
bootstrap:[MainComponent] }) export class AppModule { }
如何修复此错误以便在我的应用程序中使用引导程序,提前致谢
请将主要组件更改为
import { Component } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { AppModule } from './app.module.ts';
import 'zone.js';
@Component({
selector: 'app-root',
standalone: true,
imports: [AppModule],
template: `
Test
`,
})
export class MainComponent {
...
}
bootstrapApplication(App);
在应用程序模块中,我们需要从导入数组中删除 MainComponent,并删除引导数组
由于最新的 Angular 使用独立模块,因此无需使用 ngmodule 进行引导,而是采用此方法!