Angular Standalone Pipe with Storybook: NG0302: 该管道在组件中找不到

huangapple go评论57阅读模式
英文:

Angular Standalone Pipe with Storybook: NG0302: The pipe could not be found in the component

问题

以下是您要翻译的内容:

"Got the task to include a standalone pipe in Storybook. My Pipe as simple as:
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
    name: 'shortpipe',
    standalone: true,
})
export class ShortPipe implements PipeTransform {
    transform(value: any): any {
        return 'hello';
    }
}

Even my Storybook story is not that complicated:

const meta: Meta<any> = {
    title: 'Title of my fantastic story',
    render: () => ({
        template: '<p>{{'22222' | shortpipe}}</p>',
    }),
};

export default meta;
type Story = StoryObj<any>;

export const Default: Story = {
    render: (args) => ({
        moduleMetadata: [
            {
                imports: [ShortPipe],
            },
        ],
        template: '<p>{{'22222' | shortpipe}}</p>',
    }),
};

However I got the error:
NG0302: The pipe 'shortpipe' could not be found in the 'StorybookWrapperComponent' component. Verify that it is declared or imported in this module. Find more at https://angular.io/errors/NG0302

Angular: 15.0.2

@storybook/angular: 6.5.15"

英文:

Got the task to include a standalone pipe in Storybook. My Pipe as simple as:
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
    name: &#39;shortpipe&#39;,
    standalone: true,
})
export class ShortPipe implements PipeTransform {
    transform(value: any): any {
        return &#39;hello&#39;;
    }
}

Even my Storybook story is not that complicated:

const meta: Meta&lt;any&gt; = {
    title: &#39;Title of my fantastic story&#39;,
    render: () =&gt; ({
        template: `&lt;p&gt;{{&#39;22222&#39; | shortpipe}}&lt;/p&gt;`,
    }),
};

export default meta;
type Story = StoryObj&lt;any&gt;;

export const Default: Story = {
    render: (args) =&gt; ({
        moduleMetadata: [
            {
                imports: [ShortPipe],
            },
        ],
        template: `&lt;p&gt;{{&#39;22222&#39; | shortpipe}}&lt;/p&gt;`,
    }),
};

However I got the error:
NG0302: The pipe &#39;shortpipe&#39; could not be found in the &#39;StorybookWrapperComponent&#39; component. Verify that it is declared or imported in this module. Find more at https://angular.io/errors/NG0302

Angular: 15.0.2

@storybook/angular: 6.5.15

答案1

得分: 0

已找到答案,问题只是将moduleMetadata导入放在错误的位置。将其移至装饰器数组的顶部即可解决:

export const Default: Story = {
    decorators: [
        moduleMetadata({
            imports: [ShortPipe],
        }),
    ],
    render: (args) => ({
        template: `<p>{{'22222' | shortpipe}}</p>`,
    }),
};
英文:

Found the answer already, the issue was only placing moduleMetadata import to the wrong place. Moving it up to the decorators array fixed it:

export const Default: Story = {
   decorators: [
       moduleMetadata({
           imports: [ShortPipe],
       }),
   ],
   render: (args) =&gt; ({
       template: `&lt;p&gt;{{&#39;22222&#39; | shortpipe}}&lt;/p&gt;`,
   }),
};

huangapple
  • 本文由 发表于 2023年2月9日 02:40:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390348.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定