如何在角度故事书组件中注入服务?

时间:2019-10-31 16:06:54

标签: angular storybook angular-storybook

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) {}
 }

1 个答案:

答案 0 :(得分:0)

您需要将它们添加到metaData模块中

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

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