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

相关内容

热门资讯

黑科技教程!hhpoker德州... 黑科技教程!hhpoker德州作弊,智星德州插件怎么下载,一贯真的有挂(有挂技巧)1、下载好智星德州...
总算了解!琼崖辅助软件,闲逸斗... 总算了解!琼崖辅助软件,闲逸斗地主辅助,详细app(确实有挂)运闲逸斗地主辅助辅助工具,进入游戏界面...
2026版教学!wepoker... 2026版教学!wepoker怎么破解游戏,德扑之心免费透视,都是是有挂(有挂猫腻)1、首先打开德扑...
热点推荐!天天辅助工具,丽水都... 热点推荐!天天辅助工具,丽水都莱大菠萝辅助,详细技巧(果真有挂)1.丽水都莱大菠萝辅助 选牌创建新账...
刚刚!云扑克有透视吗,德普之星... 刚刚!云扑克有透视吗,德普之星辅助器app,竟然存在有挂(真实有挂)进入游戏-大厅左侧-新手福利-激...
热点推荐!来来舟山麻将辅助,微... 热点推荐!来来舟山麻将辅助,微乐自建房辅助工具,详细神器(有挂规律)1、许多玩家不知道微乐自建房辅助...
明白辅助挂!WePoKer辅助... 明白辅助挂!WePoKer辅助器,德普软件,原来真的有挂(有挂工具)1、金币登录送、破产送、升级送、...
六分钟了解!小程序蜀山西川血战... 六分钟了解!小程序蜀山西川血战辅助,大菠萝免费辅助,详细技巧(的确有挂)1、完成小程序蜀山西川血战辅...
据悉!wepoker辅助器最新... 据悉!wepoker辅助器最新版本更新内容,德扑之星辅助器怎么用,其实存在有挂(果真有挂)1、德扑之...
科普!开心泉州免费辅助器,随意... 科普!开心泉州免费辅助器,随意玩辅助器视频透视挂,详细神器(有挂工具)1、随意玩辅助器视频透视挂脚本...