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

相关内容

热门资讯

黑科技美元局!fishpoke... 黑科技美元局!fishpoker大菠萝外挂(黑科技ai)一直存在有挂(有挂工具)-哔哩哔哩;支持多人...
黑科技挂!德州软件辅助(黑科技... 黑科技挂!德州软件辅助(黑科技ai)原先有挂(有挂脚本)-哔哩哔哩;科技安装教程;136704302...
黑科技存在!wepoke辅助技... 黑科技存在!wepoke辅助技巧(智能ai)原先真的是有挂(有挂最新款)-哔哩哔哩,支持语音通讯、好...
黑科技辅助!cloudpoke... 黑科技辅助!cloudpoker有辅助透视么(黑科技)先前真的是有挂(有挂透明挂)-哔哩哔哩;玩家必...
黑科技科技!德州ai神器下载(... 黑科技科技!德州ai神器下载(辅助挂)其实有挂(有挂辅助挂)-哔哩哔哩1、点击下载安装,微扑克wpk...
黑科技安卓版!cloudpok... 黑科技安卓版!cloudpoker云扑克(透视)本来真的有挂(有挂打法)-哔哩哔哩;最新版2024是...
黑科技智能ai!wepoke软... 自定义wepoke软件俱乐部系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器...
黑科技脚本!微扑克ai辅助实战... 黑科技脚本!微扑克ai辅助实战效果(ai辅助)确实真的有挂(有挂ai代打)-哔哩哔哩;实战中需综合运...
黑科技系统!wpk透视辅助合作... 黑科技系统!wpk透视辅助合作(黑科技)最初是真的有挂(有挂俱乐部)-哔哩哔哩;人气非常高,ai更新...
黑科技安卓版!wepoke透明... 黑科技安卓版!wepoke透明挂要多少钱(黑科技ai)切实存在有挂(有挂软件)-哔哩哔哩;1分钟了解...