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

相关内容

热门资讯

网友热议!皮皮跑子辅助(辅助挂... 皮皮跑子辅助是一款专注玩家量身打造的游戏记牌类型软件,在皮皮跑子辅助这款游戏中我们可以记录下每张牌的...
第九分钟开挂!丰城瓜瓜棋牌辅助... 第九分钟开挂!丰城瓜瓜棋牌辅助,永胜联盟辅助软件,2025版教程-2026最新版本进入游戏-大厅左侧...
据公告内容!贪玩娱乐科技(辅助... 您好:贪玩娱乐科技这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别...
4分钟开挂!闲逸辅助app下载... 您好,闲逸辅助app下载这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多...
此事迅速冲上热搜!掌中乐游戏中... 此事迅速冲上热搜!掌中乐游戏中心破解版(辅助挂)原来确实有挂(有挂模板)-哔哩哔哩;打开点击测试直接...
7分钟开挂!盛世辅助工具,赣湘... 7分钟开挂!盛世辅助工具,赣湘互娱辅助,解密教程-2026最新版本1、起透看视 赣湘互娱辅助透明视辅...
目前来看!天天爱柳州麻将辅助器... 大家好,今天小编来为大家解答天天爱柳州麻将辅助器这个问题咨询软件客服可以免费测试直接加微信(1367...
八分钟开挂!友友邳州辅助软件下... 八分钟开挂!友友邳州辅助软件下载,多乐手游辅助,解密教程-2026最新版本;所有人都在同一条线上,像...
现有说明如下!永胜联盟辅助软件... 您好:永胜联盟辅助软件这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌...
第4分钟开挂!情怀蒲仙辅助,广... 第4分钟开挂!情怀蒲仙辅助,广西友乐辅助器,揭秘教程-2026最新版本1、广西友乐辅助器系统规律教程...