【问题标题】:How to inject services in angular storybook components?如何在 Angular Storybook 组件中注入服务?
【发布时间】:2020-02-27 01:04:50
【问题描述】:

Github:https://github.com/danielhdz56/storybook-demo.git

在组件中注入服务总是会在故事书中引发错误:

Error: Can't resolve all parameters for CustomService: (?).

一个示例组件:

 @Component({
   selector: 'app-custom-selector',
   templateUrl: './custom-selector.component.html',
   styleUrls: ['./custom-selector.component.scss'],
 })
 export class NavigationItemComponent {
   constructor(private customService: CustomService) {}
 }

【问题讨论】:

标签: angular storybook angular-storybook


【解决方案1】:

你需要将它们添加到模块元数据中

const modules = {
    imports: [..],
    providers: [CustomService],
    declarations: [NavigationItemComponent ]
};

// Then in stories: 
.add('description',
    () => ({
        component: NavigationItemComponent ,
        props: {...},
        moduleMetadata: modules
    }
)

【讨论】:

猜你喜欢
  • 2018-10-27
  • 1970-01-01
  • 2018-08-22
  • 2018-05-20
  • 2016-03-25
  • 1970-01-01
  • 2021-07-23
  • 2020-01-06
  • 2016-08-08
相关资源
最近更新 更多