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

你

相关内容

热门资讯

安装教程!黑侠破解wepoke... 『标题:当前标题』主打赛后复盘的 AI 游戏攻略黑科技走红,对局总结能力开挂。上传对局回放或者战绩截...
玩家实测!wepokerplu... AI 黑科技赛道又上新,这款智能处理工具堪称效率开挂,专门面向短视频创作者打造。它打通脚本撰写、AI...
我来分享!wepoker辅助器... 您好,我来分享!wepoker辅助器有哪些功能,wepoker透视开挂助手(一向真的有开挂)这款游戏...
避坑细节!wepoker底牌透... 您好,避坑细节!wepoker底牌透视,德扑之星真的有透视吗安装(都是有透视挂)这款游戏可以开挂的,...
三分钟了解!wpk是真的还是假... 三分钟了解!wpk是真的还是假的,wepoker真的有挂安装(本来是真的有辅助挂)AI 是一款可以让...
科普攻略!德普之星怎么作弊,微... 科普攻略!德普之星怎么作弊,微扑克透视软件脚本(从前有辅助)主打本地运行的 AI 黑科技工具迎来新版...
透牌教程!wepoker有没有... 很多人使用云端 AI 时,都担心wepoker有没有挂、内部资料上传带来隐私风险。这款本地运行的 A...
记者爆料!WPK真假,wepo... AI 黑科技赛道又上新,这款智能处理工具堪称效率开挂,专门面向短视频创作者打造。它打通脚本撰写、AI...
重要通知!wpk官网下载链接,... 『标题:当前标题』主打赛后复盘的 AI 游戏攻略黑科技走红,对局总结能力开挂。上传对局回放或者战绩截...
交流学习经验!wepokerp... 交流学习经验!wepokerplus作弊,WPK开挂脚本平台(起初是有辅助)AI 智能体黑科技再上新...