[英]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.