简体   繁体   English

Angular Storybook 中的 FormGroup:将循环结构转换为 JSON

[英]Angular FormGroup in Storybook: Converting circular structure to JSON

I'm work with angular and storybook.我正在使用 angular 和故事书。 I have a FormGroup and FormArray in my model but they are not working with storybook.我的 model 中有一个 FormGroup 和 FormArray ,但它们不适用于故事书。

a.stories.ts -> a.stories.ts ->

import { CommonModule } from '@angular/common';
import { FormArray, FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import { Meta, moduleMetadata, Story } from '@storybook/angular';

export default {
    title: 'Example/A',
    component: AComponent,
    decorators: [
        moduleMetadata({
            imports: [
                CommonModule,
                ReactiveFormsModule,
            ],
            providers: [],
        }),
    ],
} as Meta;

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

export const Costs = Template.bind({});
Costs.args = {
    model: {
        questions: [
            {
                ...,
                "question": "lorem ipsum",
                "formGroup": new FormGroup({
                    answers: new FormArray([
                        new FormGroup({
                            "Q020_A001": new FormControl(null, [Validators.required]),
                            "Q020_A002": new FormControl(null, [Validators.required]),
                        }),
                    ]),
                }),
                "answers": [
                    {
                        ...,
                        "answerCode": "Q020_A001",
                    },
                    {
                        ...,
                        "answerCode": "Q020_A002",
                    },
                ],
            }
        ],
    },
};

I get an error in storybook ->我在故事书中遇到错误->

TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'FormGroup'
    |     property 'controls' -> object with constructor 'Object'
    |     property 'answers' -> object with constructor 'FormArray'
    --- property '_parent' closes the circle
    at JSON.stringify (<anonymous>)

It works if "formGroup" is empty.如果“formGroup”为空,它可以工作。 -> ->

            "formGroup": new FormGroup({
            }),

But if "formGroup" is not empty, it won't work.但是如果“formGroup”不为空,它就不起作用。 -> ->

        "formGroup": new FormGroup({
            asd: new FormControl(''),
        }),

How can I fix this?我怎样才能解决这个问题?

The problem is for every args you provided to Storybook, SB gonna call JSON.stringify .问题在于您提供给 Storybook 的每个args ,SB 会调用JSON.stringify This design is required because SB also use your args at runtime and allow you to change value.这种设计是必需的,因为 SB 在运行时也使用您的args并允许您更改值。

In your case, you create a Reactive forms model which cause the object to be not able to convert to string by JSON.stringify .在您的情况下,您创建了一个反应式 forms model 导致 object 无法通过JSON.stringify转换为字符串。

To fix it, you need to:要修复它,您需要:

  1. Provide args to SB as raw data only仅将参数作为原始数据提供给 SB
  2. Change logic in your AComponent to create model from data provided above更改AComponent中的逻辑以根据上面提供的数据创建 model

Sample code:示例代码:

/// a.stories.ts
Costs.args = {
    model: {
        questions: [
            {
                ...,
                "question": "lorem ipsum",
                "formGroup": {
                    answers: [
                        {
                            "Q020_A001": null,
                            "Q020_A002": null,
                        }),
                    ]),
                },
                "answers": [
                    {
                        ...,
                        "answerCode": "Q020_A001",
                    },
                    {
                        ...,
                        "answerCode": "Q020_A002",
                    },
                ],
            }
        ],
    },
};
/// A.component.ts

export class AComponent {
  @Input() model;

  constructor() {
     // create your reactive forms here with data from model
     this.form = new FormGroup(...)
  }
}

I solved the problem by creating a new AStoryComponent.我通过创建一个新的 AStoryComponent 解决了这个问题。

// a-story.component.ts

@Component({
    selector: 'a-component-story',
    template: `<a-component [model]="model"></a-component>`,
})
export class AStoryComponent implements OnInit {

    @Input() model!: any;

    constructor() {}

    ngOnInit() {
        // Converts the "formGroup" property I specified in a.stories.ts to the real formGroup object.
        convertStoryModelToRealModel(this.model);
    }

}


// a.stories.ts

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

export default {
    title: 'Example/A',
    component: AStoryComponent,
    decorators: [
        moduleMetadata({
            declarations: [AStoryComponent, AComponent],
            imports: [
                CommonModule,
                ReactiveFormsModule,
            ],
        }),
    ],
} as Meta;

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

export const Costs = Template.bind({});
Costs.args = {
    model: {
        questions: [
            {
                ...,
                "question": "lorem ipsum",
                "formGroup": {
                    answers: [
                        {
                            "Q020_A001": [null, [Validators.required]],
                            "Q020_A002": [null, [Validators.required]],
                        },
                    ],
                },
                "answers": [
                    {
                        ...,
                        "answerCode": "Q020_A001",
                    },
                    {
                        ...,
                        "answerCode": "Q020_A002",
                    },
                ],
            }
        ],
    } as any,
};

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

相关问题 错误TypeError:将圆形结构转换为JSON-&gt;从具有构造函数&#39;FormGroup&#39;的对象开始 - ERROR TypeError: Converting circular structure to JSON --> starting at object with constructor 'FormGroup' Angular-TypeError:将圆形结构转换为JSON - Angular - TypeError: Converting circular structure to JSON Angular5 Universal:将圆形结构转换为JSON - Angular5 Universal: Converting circular structure to JSON TypeError:将圆形结构转换为JSON angular 6 - typeerror: converting circular structure to json angular 6 Angular Json Pipe 类型错误:将循环结构转换为 Z0ECD11C1D7A287401D148A23BBDZ - Angular Json Pipe TypeError: Converting circular structure to JSON 将循环结构转换为Angular2 Form中的JSON EXCEPTION - Converting circular structure to JSON EXCEPTION in Angular2 Form 未捕获的类型错误:在 Angular / 依赖项更新后将循环结构转换为 JSON - Uncaught TypeError: Converting circular structure to JSON after Angular / dependencies update UnhandledPromiseRejectionWarning: TypeError: 使用 Jest + Angular 将循环结构转换为 JSON - UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON with Jest + Angular Angular FormControl Jest 测试导致 TypeError:将循环结构转换为 JSON - Angular FormControl Jest test causing TypeError: Converting circular structure to JSON 在Stripe中将圆形结构转换为JSON - Converting circular structure to JSON in Stripe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM