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

你

相关内容

热门资讯

WPK官网!WPK教程辅助ai... WPK官网!WPK教程辅助ai,约局吧辅助器,确实真的有ai辅助指南1、不需要AI权限,帮助你快速的...
WPK俱乐部!AApoker教... WPK俱乐部!AApoker教程辅助ai,有哪些免费的wpk作弊码,真是真的有ai辅助模块1、有哪些...
WPK透视!WePoKerap... WPK透视!WePoKerapp开挂ai,AApoker控制牌,一贯有ai开挂项目1、每一步都需要思...
WPK透视!微扑克神器辅助器a... WPK透视!微扑克神器辅助器ai,aapoker透视脚本下载,竟然真的是有ai辅助器课程aapoke...
WPK作弊!智星菠萝德州攻略辅... WPK作弊!智星菠萝德州攻略辅助ai,poker红龙辅助,果然是有ai辅助课程1、进入游戏-大厅左侧...
WPK靠谱!红龙PoKer插件... WPK靠谱!红龙PoKer插件辅助器ai,wpk透视是真的假的,原来存在有ai辅助器总结1、实时wp...
WPK真假!德州扑克方法辅助a... WPK真假!德州扑克方法辅助ai,佛手在线是不是有挂,切实是真的有ai辅助策略小薇(辅助器软件下载)...
WPK四人!wpk教程开挂ai... WPK四人!wpk教程开挂ai,德普之星透视,其实存在有ai开挂妙招1、完成德普之星透视有辅助插件,...
WPK工具!红龙PoKer神器... WPK工具!红龙PoKer神器开挂ai,werplan怎么作弊,切实是有ai开挂手段小薇(辅助器软件...
WPK控制牌!aapoker技... WPK控制牌!aapoker技巧透视挂ai,德普之星透视,原来真的是有ai透视挂课程所有人都在同一条...