使用Storybook渲染独立的Angular Material选择组件?

huangapple go评论106阅读模式
英文:

Rendering Angular standalone Angular Material select components with Storybook?

问题

尝试使用Storybook呈现自定义的独立Angular Material选择组件。

它产生了以下错误:

  1. - 在您的应用程序中导入了`BrowserAnimationsModule``NoopAnimationsModule`
  2. - `@Component`装饰器的`animations`字段中定义了名为`@transitionMessages`的动画的相应配置(请参见https://angular.io/api/core/Component#animations)。
  3. 在检查NoSyntheticProp时出现(platform-browser.mjs:659:1
  4. NoneEncapsulationDomRenderer.setProperty时出现(platform-browser.mjs:642:1
  5. elementPropertyInternal时出现(core.mjs:10801:1
  6. Module.ɵɵproperty时出现(core.mjs:13521:1
  7. MatFormField_div_17_Template时出现(form-field.mjs:26:1
  8. 在执行模板时出现(core.mjs:10441:1
  9. 在刷新视图时出现(core.mjs:10326:1
  10. 在刷新嵌入式视图时出现(core.mjs:11339:1
  11. 在刷新视图时出现(core.mjs:10350:1
  12. 在刷新组件时出现(core.mjs:11385:1

这是因为组件是独立的,所以应该使用独立API包含BrowserAnimationsModule,就像这样:

  1. providers: [provideRouter(routes), provideAnimations(), provideHttpClient()],
  2. });

然而,Storybook正在引导应用程序,所以如何调用provideAnimations()

英文:

Trying to render a custom standalone Angular Material select component with Storybook.

It produces the error:

  1. ERROR Error: Unexpected synthetic property @transitionMessages found. Please make sure that:
  2. - Either `BrowserAnimationsModule` or `NoopAnimationsModule` are imported in your application.
  3. - There is corresponding configuration for the animation named `@transitionMessages` defined in the `animations` field of the `@Component` decorator (see https://angular.io/api/core/Component#animations).
  4. at checkNoSyntheticProp (platform-browser.mjs:659:1)
  5. at NoneEncapsulationDomRenderer.setProperty (platform-browser.mjs:642:1)
  6. at elementPropertyInternal (core.mjs:10801:1)
  7. at Module.ɵɵproperty (core.mjs:13521:1)
  8. at MatFormField_div_17_Template (form-field.mjs:26:1)
  9. at executeTemplate (core.mjs:10441:1)
  10. at refreshView (core.mjs:10326:1)
  11. at refreshEmbeddedViews (core.mjs:11339:1)
  12. at refreshView (core.mjs:10350:1)
  13. at refreshComponent (core.mjs:11385:1)

And this is something that happens because the component is standalone, and so the BrowserAnimationsModule should be included using the standalone API like this:

  1. bootstrapApplication(AppComponent, {
  2. providers: [provideRouter(routes), provideAnimations(), provideHttpClient()],
  3. });

However Storybook is bootstrapping the application, so how would be go about calling provideAnimations()?

答案1

得分: 5

请看以下示例,了解在哪里添加提供程序:

  1. import { Meta, applicationConfig } from '@storybook/angular';
  2. import { ProjectsTableComponent } from './projects-table.component';
  3. import { provideAnimations } from '@angular/platform-browser/animations';
  4. export default {
  5. title: 'Presentational/Admin/Projects Table',
  6. component: ProjectsTableComponent,
  7. decorators: [
  8. applicationConfig({
  9. providers: [provideAnimations()],
  10. }),
  11. ],
  12. } as Meta<ProjectsTableComponent>;
  13. export const WithData = {
  14. render: (args: ProjectsTableComponent) => ({
  15. props: args,
  16. }),
  17. args: {
  18. projects: getProjectsData(),
  19. },
  20. };

在上述代码中,decorators 部分是用来添加提供程序的示例。

英文:

See decorators and applicationConfig below as an example of where to add providers:

  1. import { Meta, applicationConfig } from &#39;@storybook/angular&#39;;
  2. import { ProjectsTableComponent } from &#39;./projects-table.component&#39;;
  3. import { provideAnimations } from &#39;@angular/platform-browser/animations&#39;;
  4. export default {
  5. title: &#39;Presentational/Admin/Projects Table&#39;,
  6. component: ProjectsTableComponent,
  7. decorators: [
  8. applicationConfig({
  9. providers: [provideAnimations()],
  10. }),
  11. ],
  12. } as Meta&lt;ProjectsTableComponent&gt;;
  13. export const WithData = {
  14. render: (args: ProjectsTableComponent) =&gt; ({
  15. props: args,
  16. }),
  17. args: {
  18. projects: getProjectsData(),
  19. },
  20. };

huangapple
  • 本文由 发表于 2023年5月25日 04:22:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76327161.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定