typescript 将Angular Material Select Demo Stackblitz制作成独立组件?

yws3nbqq  于 2023-05-08  发布在  TypeScript
关注(0)|答案(1)|浏览(115)

尝试将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)],
});

并使组件像这样独立(添加standaloneimports。导入来自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被删除,就会产生其他问题。
有什么解决办法吗?

cngwdvgl

cngwdvgl1#

当使用bootstrapApplication函数引导应用程序时,我们需要在main.ts中提供动画模块

main.ts

bootstrapApplication(SelectFormExample, {
  providers: [
    provideAnimations(),
    provideHttpClient()
  ],
});

Reference
Working Example

相关问题