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

你

相关内容

热门资讯

2027版透视挂!天府棋牌雀神... 2027版透视挂!天府棋牌雀神山庄麻将透视挂多功能AI攻略,天府棋牌雀神山庄麻将竟然是真的有透视挂攻...
WPK链接透视挂学习!wpk辅... WPK链接透视挂学习!wpk辅助软件,微扑克ai多功能透视挂app1、打开软件启动之后找到中间准星的...
技术分享辅助器!来三上海麻将a... 您好,技术分享辅助器!来三上海麻将ai辅助器多功能插件,好像真的是有辅助器方法大纲这款游戏可以开挂的...
2027版开挂!乐乐阜阳麻将开... 2027版开挂!乐乐阜阳麻将开挂多功能AI教程,乐乐阜阳麻将本来真的有开挂教程1)乐乐阜阳麻将辅助插...
WPK工具辅助总结!wpk俱乐... WPK工具辅助总结!wpk俱乐部辅助器,wpkai多功能辅助攻略1、下载好wpk俱乐部辅助器正确养号...
分享给玩家开挂!江岸多玩ai开... 分享给玩家开挂!江岸多玩ai开挂多功能插件,果然真的有开挂神器法门是一款可以让一直输的玩家,快速成为...
2027版透视器!好彩麻将透视... 2027版透视器!好彩麻将透视器多功能AI脚本,好彩麻将真是真的有透视器脚本1、不需要AI权限,帮助...
WPK辅助透视方式!wpk安卓... WPK辅助透视方式!wpk安卓下载辅助,aapokerai多功能透视插件wpk安卓下载辅助透视方法中...
玩家必看分享辅助器!闲来贵州麻... 玩家必看分享辅助器!闲来贵州麻将ai辅助器多功能插件,确实真的有辅助器安装指南是一款可以让一直输的玩...
2027版辅助!江淮竞技麻将辅... 2027版辅助!江淮竞技麻将辅助多功能AI教程,江淮竞技麻将竟然真的是有辅助教程1、江淮竞技麻将免费...