尝试将Angular Material Select Demo制作成独立组件。
This is the Stackblitz.
这些是步骤。
将main.ts
替换为以下内容(用于引导独立组件):
import { bootstrapApplication } from '@angular/platform-browser';
import { provideRouter, Routes } from '@angular/router';
import { SelectFormExample } from './app/select-form-example';
const routes: Routes = [];
bootstrapApplication(SelectFormExample, {
providers: [provideRouter(routes)],
});
并使组件像这样独立(添加standalone
和imports
。导入来自app.module.ts
):
/**
* @title Select in a form
*/
@Component({
standalone: true,
imports: [
FormsModule,
HttpClientModule,
MatNativeDateModule,
ReactiveFormsModule,
MaterialExampleModule,
CommonModule,
BrowserModule,
BrowserAnimationsModule,
],
selector: 'select-form-example',
templateUrl: 'select-form-example.html',
})
在这个阶段,它编译得很好,但抛出错误:
ROR Error: Providers from the `BrowserModule` have already been loaded. If you need access to common directives such as NgIf and NgFor, import the `CommonModule` instead.
CommonModule
已经包含在内,如果BrowserModule
被删除,就会产生其他问题。
有什么解决办法吗?
1条答案
按热度按时间cngwdvgl1#
当使用bootstrapApplication函数引导应用程序时,我们需要在main.ts中提供动画模块
main.ts
Reference
Working Example