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

相关内容

热门资讯

第8分钟带你科普!欢聚水鱼智能... 第8分钟带你科普!欢聚水鱼智能辅助,奇迹陕西辅助,揭秘教程(有挂细节)-哔哩哔哩;无需打开直接搜索加...
第一分钟带你科普!博乐填大坑图... 第一分钟带你科普!博乐填大坑图片,福州十八扑外卦,曝光教程(有挂规律)-哔哩哔哩;无需打开直接搜索加...
四分钟带你讲解!潮汕来物局开挂... 潮汕来物局开挂方法 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大理由: ...
第三分钟带你透视!潮汕透视辅助... 第三分钟带你透视!潮汕透视辅助,决战卡五星开挂方法,必赢教程(有挂规律)-哔哩哔哩 【无需打开直接搜...
第8分钟带你开挂!天胡辅助脚本... 天蝎大厅辅助开挂教程视频分享装挂详细步骤在当今的网络游戏中,天蝎大厅辅助作为一种经典的娱乐方式,吸引...
第8分钟带你辅助!微信茶馆辅助... 第8分钟带你辅助!微信茶馆辅助器,全来潜山跑风破解版安卓,AI教程(揭秘有挂)-哔哩哔哩全来潜山跑风...
第五分钟带你发现!微信微乐游戏... 【亲,微信微乐游戏辅助脚本 这款游戏可以开挂的,确实是有挂的,很多玩家在这款微信微乐游戏辅助脚本中打...
5分钟带你辅助!wepoker... 5分钟带你辅助!wepoker插件程序,兴动互娱辅助器视频,大神讲解(新版有挂)-哔哩哔哩【无需打开...
第九分钟带你透视!新上游辅助,... 第九分钟带你透视!新上游辅助,悠闲卡五星辅助,攻略教程!(存在有挂)-哔哩哔哩1、下载安装好悠闲卡五...
十分钟带你透视!湖湘互娱牛牛,... 十分钟带你透视!湖湘互娱牛牛,胡乐辅助脚本,实用技巧(证实有挂)-哔哩哔哩>>您好:软件加薇1367...