简体   繁体   English

具有依赖关系的组件的故事书故事的基本示例

[英]Basic example for storybook story for component with dependency

Am new to storybook and the docs make it pretty clear on the idiomatic way to write stories in Angular.我是故事书的新手, 文档清楚地说明了在 Angular 中编写故事的惯用方式。 Here's an example of one for my app:这是我的应用程序的一个示例:

import { Meta, Story } from '@storybook/angular';

import {CustomerListComponent} from "@app/modules/customer/customer-search/components/customer-list/customer-list.component";

export default {
  title: 'Customer List',
  component: CustomerListComponent,
} as Meta;


const Template: Story = (args) => ({
  props: args,
});

export const EmptyArgs = Template.bind({});
EmptyArgs.args = {};

This fails with the following error:这失败并出现以下错误:

在此处输入图像描述

Indeed, if we look at the constructor for the CustomerListComponent there is a dependency on CustomerSearchComponent .事实上,如果我们查看CustomerSearchComponent的构造函数,就会发现CustomerListComponent存在依赖关系。

@Component({
  selector: 'customer-list',
  templateUrl: './customer-list.component.html',
  styleUrls: ['./customer-list.component.scss']
})
export class CustomerListComponent {
  constructor (
    private customerSearchComponent: CustomerSearchComponent,
  ) {
  }
...

So what is typically done in this case?那么在这种情况下通常会做什么呢? Can I just tell storybook to load that component, and any dependencies it has and so forth?我可以告诉故事书加载该组件,以及它具有的任何依赖项等等吗? Do I need to pass a mock of CustomerSearchComponent somehow?我是否需要以某种方式传递CustomerSearchComponent的模拟? Am having trouble finding clean examples of cases like this, storybook docs seem to assume components with no depenencies on other components.很难找到这样的案例的干净示例,故事书文档似乎假设组件不依赖于其他组件。

This also has me thinking - should I not plan to use storybook to render larger views (ie. parents with several child components)?这也让我思考 - 我是否应该不打算使用故事书来呈现更大的视图(即具有多个子组件的父母)? Is that going to be hard to configure.是不是很难配置。


PS: random question - is storiesOf syntax deprecated? PS:随机问题 - 不推荐使用storiesOf语法吗? It's not in official docs but tutorials all over the web.它不在官方文档中,而是在 web 中的教程。

Based on what you've posted, this probably could help you, can you try on your end and see if adding the search component as well will render the component.根据您发布的内容, 可能会对您有所帮助,您可以尝试一下,看看添加搜索组件是否也会呈现该组件。

Also, to answer your question related to the storiesOf syntax, it's deprecated to the majority of the supported frameworks, with the exception of React Native.此外,为了回答您与 storiesOf 语法相关的问题,除了 React Native 之外,大多数受支持的框架都已弃用它。 Based on the feedback from the maintainer React Native will be getting an update soon removing it根据维护者的反馈,React Native 将很快获得更新,将其删除

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM