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

你

相关内容

热门资讯

正版透视器玲珑透视器多功能AI... 正版透视器玲珑透视器多功能AI教程,玲珑总是真的有透视器教程1、实时玲珑透视辅助更新:用户可以随时随...
正版开挂天天摸麻将开挂多功能A... 正版开挂天天摸麻将开挂多功能AI工具,天天摸麻将果然是有开挂工具在进入天天摸麻将软件靠谱后,参与本局...
正版外挂星聚汇外挂多功能AI脚... 正版外挂星聚汇外挂多功能AI脚本,星聚汇确实有外挂脚本1、完成星聚汇有辅助插件,帮助玩家取得所有比赛...
正版开挂盛世开挂多功能AI软件... 正版开挂盛世开挂多功能AI软件,盛世一直真的有开挂软件盛世辅助器是一种具有地方特色的麻将游戏,要想赢...
正版外挂团团竞技外挂多功能AI... 正版外挂团团竞技外挂多功能AI工具,团团竞技确实是真的有外挂工具1、许多玩家不知道团团竞技辅助怎么退...
正版外挂趣玩娱乐外挂多功能AI... 正版外挂趣玩娱乐外挂多功能AI攻略,趣玩娱乐好像真的是有外挂攻略1、每一步都需要思考,不同水平的挑战...
正版辅助哥哥打大A辅助多功能A... 您好,哥哥打大A这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这...
正版外挂胡乐安庆麻将外挂多功能... 正版外挂胡乐安庆麻将外挂多功能AI攻略,胡乐安庆麻将一贯真的有外挂攻略1、金币登录送、破产送、升级送...
正版开挂川渝互娱开挂多功能AI... 正版开挂川渝互娱开挂多功能AI攻略,川渝互娱竟然是有开挂攻略1、进入到川渝互娱是否有挂之后,能看到左...
正版辅助器兴动互娱辅助器多功能... 正版辅助器兴动互娱辅助器多功能AI插件,兴动互娱竟然存在有辅助器插件1、在兴动互娱插件功能辅助器技巧...