简体   繁体   English

在 angular 模块中使用注入的故事书

[英]Storybook with using Injection in angular module

I want to use Storybook to develop and test my UI components and release them as npm library from within the same project.我想使用 Storybook 开发和测试我的 UI 组件,并将它们作为 npm 库从同一个项目中发布。 This is why I encapsulate all components as a single feature module.这就是我将所有组件封装为单个功能模块的原因。 I wanted to create a simple module which generates a form and allows some undo functionality.我想创建一个简单的模块来生成一个表单并允许一些撤消功能。 My problem is that Storybook cannot inject the Formbuilder Service into my component.我的问题是 Storybook 无法将 Formbuilder 服务注入到我的组件中。

My setup is as follows:我的设置如下:

The story:故事:

storiesOf('UndoForm', module)
  .addDecorator(
    moduleMetadata({
      imports: [ReactiveFormsModule, UndoFormModule]
    }),
  )
  .add('Testform', () => ({
    template: '<mods-undo-form></mods-undo-form>'
  })
);

The UndoFormModule: UndoForm 模块:

@NgModule({
  declarations: [UndoFormComponent],
  imports: [ReactiveFormsModule],
  exports: [UndoFormComponent]
})
export class UndoFormModule {}

The UndoFormComponent: UndoForm 组件:

@Component({
  selector: 'mods-undo-form',
  templateUrl: './undo-form.component.html',
  styleUrls: ['./undo-form.component.scss']
})
export class UndoFormComponent implements OnInit {
  [...]
  constructor(private fb: FormBuilder) { }
  [...]
}

The error I get is:我得到的错误是:

Can't resolve all parameters for UndoFormComponent: (?).无法解析 UndoFormComponent 的所有参数:(?)。

What I found out is, that when I use the @Inject annotation explicitly, then the code works:我发现,当我显式使用@Inject注释时,代码会起作用:

constructor(@Inject(FormBuilder) private fb: FormBuilder) { }

Is there any possibility to prevent the usage of the explicit annotation?是否有可能阻止使用显式注释?

You need to add "emitDecoratorMetadata": true in compilerOptions object in .storybook/tsconfig.json file.您需要在.storybook/tsconfig.json文件中的compilerOptions对象中添加"emitDecoratorMetadata": true

So your .storybook/tsconfig.json should be like this:所以你的.storybook/tsconfig.json应该是这样的:

{
  "extends": "../tsconfig.app.json",
  "compilerOptions": {
    "emitDecoratorMetadata": true,   <--------- Add this!
    ...
  },
  ...
}

Then make sure to restart your storybook process.然后确保重新启动您的故事书过程。

You can import it directly into the story.您可以将其直接导入到故事中。 like so:像这样:

import { BannerV2Component } from './banner.v2.component';
import { moduleMetadata } from '@storybook/angular';


export default { 
  title: 'Banner',
  decorators: [
    moduleMetadata({
        imports: [ReactiveFormsModule, UndoFormModule],
        providers: [FormBuilder],
    })
  ]
}
export const OptionOne = () => ({
  component: BannerV2Component,
  props: {
    mainText:'Text Two',
    showBreadcrumbs:true,
  },
});

You need to provide 'FormBuilder' in the providers array in moduleMetadata of the storybook decorator.您需要在故事书装饰器的 moduleMetadata 中的提供者数组中提供“FormBuilder”。 This way, your storybook setup will recognise that FormBuilder is being used as an injectable dependency in your component.这样,您的故事书设置将识别 FormBuilder 被用作组件中的可注入依赖项。

Here is how I am using it in one of our stories:以下是我在我们的一个故事中如何使用它:

storiesOf('UndoForm', module)
  .addDecorator(
    moduleMetadata({
      imports: [ReactiveFormsModule, UndoFormModule],
      providers: [FormBuilder],
    }),
  )
  .add('Testform', () => ({
    template: '<mods-undo-form></mods-undo-form>'
  })
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Storybook 6 中使用 TailwindCss 和 Angular - Using TailwindCss in Storybook 6 with Angular Angular 8 带故事书 6 - Angular 8 with Storybook 6 Angular 6和故事书 - Angular 6 and Storybook 如何将 Storybook 与使用 [routerLink] (Angular) 的组件一起使用? - How to use Storybook with a component using [routerLink] (Angular)? 使用 Storybook 和 Cypress 测试 angular 组件@Output - Test angular component @Output using Storybook and Cypress 与 Angular 依赖注入与惰性模块混淆 - Confused with Angular Dependency Injection with lazy module Angular 依赖注入查找和模块注入器层次结构 - Angular dependecy injection lookup and module injector hierarchy 使用模块联合架构导入 angular 应用程序时,“必须从注入上下文调用 inject()” - "inject() must be called from an injection context" when importing angular app using module federation architecture 在故事书 5.2 中使用装饰器包装故事 - 打字稿/角度 - Wrapping a story using decorator in storybook 5.2 - typescript / angular 使用带有故事书和角度的全局样式表 - SassError: SassError: expected &quot;{&quot; - using global stylesheets with storybook & angular - SassError: SassError: expected "{"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM