AngularStorybook-有没有一种自动生成.stories.ts文件的方法?
创始人
2024-10-29 16:30:38
0

使用Angular Schematics可以自动生成Angular组件和storybook配置,从而自动生成.story.ts文件。下面是一些示例代码:

首先,安装Angular Schematics:

npm install -g @angular-devkit/schematics-cli

接着,使用以下命令生成一个新的Schematic项目:

schematics blank ../my-schematic

然后,在你的项目中添加Angular Schematics依赖:

npm install @storybook/angular @angular/cdk @ng-packagr/core

现在我们需要编写我们的schematic,用来生成以下文件:

  • src/app/components/my-component/my-component.component.ts
  • src/app/components/my-component/my-component.component.html
  • src/app/components/my-component/my-component.component.scss
  • src/app/components/my-component/my-component.component.spec.ts
  • src/app/components/my-component/my-component.stories.ts

以下是示例代码:

import { Rule, SchematicContext, Tree, chain, url, apply, move, mergeWith, applyTemplates } from '@angular-devkit/schematics';
import { strings } from '@angular-devkit/core';
import { normalize } from 'path';

// You don't have to export the function as default. You can also have more than one rule factory
// per file.
export function mySchematic(_options: any): Rule {
  return (tree: Tree, _context: SchematicContext) => {
    return chain([
      generateComponent(_options),
      generateStorybook(_options)
    ])(tree, _context);
  };
}

function generateComponent(options: any): Rule {
  const templateSource = apply(url('./files'), [
    applyTemplates({
      ...strings,
      ...options,
    }),
    move(
      normalize(`src/app/components/${options.name}`)
    )
  ]);

  return mergeWith(templateSource);
}

function generateStorybook(options: any): Rule {
  const templateSource = apply(url('./files'), [
    applyTemplates({
      ...strings,
      ...options,
    }),
    move(
      normalize(`src/app/components/${options.name}`)
    )
  ]);

  return mergeWith(templateSource);
}

相关内容

热门资讯

据文件显示!智星德州有脚本吗,... 据文件显示!智星德州有脚本吗,德扑之星私人局辅助器,总是真的是有挂(揭秘有挂)1、进入到德扑之星私人...
终于清楚!枫叶辅助脚本,新财神... 终于清楚!枫叶辅助脚本,新财神正版辅助挂,详细软件(有挂解惑)1、操作简单,无需新财神正版辅助挂手机...
此事引发广泛关注!wepoke... 此事引发广泛关注!wepoker数据分析,德扑圈有透视吗,本来真的是有挂(有挂规律)1、下载好德扑圈...
指导大家!老友广东辅助器,亲友... 指导大家!老友广东辅助器,亲友联盟能开挂不,详细挂(确实有挂)亲友联盟能开挂不透视方法中分为三种模型...
2026版总结!HH平台挂,德... 2026版总结!HH平台挂,德扑之星私人局辅助器,一直真的是有挂(的确有挂)1、实时德扑之星私人局辅...
一起来讨论!陕麻圈插件开挂,微... 一起来讨论!陕麻圈插件开挂,微乐智能辅助软件是真的吗,详细神器(详细教程)1、游戏颠覆性的策略玩法,...
黑科技辅助挂!聚星ai辅助工具... 黑科技辅助挂!聚星ai辅助工具收费多少,红龙poker辅助,本来真的是有挂(有挂教学)1、很好的工具...
我来教教大家!乐酷辅助,海洋麻... 我来教教大家!乐酷辅助,海洋麻将辅助器,详细脚本(有挂技巧)1、海洋麻将辅助器透视辅助软件激活码多个...
近日!德州机器人代打脚本,德扑... 近日!德州机器人代打脚本,德扑圈有透视吗,原来存在有挂(有挂方法)运德扑圈有透视吗辅助工具,进入游戏...
最新通报!蜀山四川智能辅助软件... 最新通报!蜀山四川智能辅助软件,微乐云南小程序辅助器,详细软件(有挂方法)1)微乐云南小程序辅助器免...