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

相关内容

热门资讯

第八分钟推荐!闽游辅助软件,顺... 第八分钟推荐!闽游辅助软件,顺欣茶楼辅助软件(原来真的是有挂)-哔哩哔哩1、顺欣茶楼辅助软件ai辅助...
第一分钟外挂!蘑菇云游破解版,... 第一分钟外挂!蘑菇云游破解版,途游辅助官网,技巧教程(真是有挂)-哔哩哔哩1、蘑菇云游破解版ai机器...
第六分钟解迷!兴动辅助器,新5... 第六分钟解迷!兴动辅助器,新518互游插件下载(果然真的是有挂)-哔哩哔哩1)新518互游插件下载辅...
第3分钟外挂!决战卡五星辅助修... 第3分钟外挂!决战卡五星辅助修改器,福建天天开心辅助器真的假的,2025新版(有挂秘籍)-哔哩哔哩;...
1分钟揭露!微信开心泉州辅助,... 1分钟揭露!微信开心泉州辅助,新九五辅助(竟然存在有挂)-哔哩哔哩微信开心泉州辅助辅助器中分为三种模...
1分钟外挂!乐酷有透视吗,牛总... 1分钟外挂!乐酷有透视吗,牛总管辅助器,我来教教你(有挂细节)-哔哩哔哩1、每一步都需要思考,不同水...
第9分钟必备!闲来山水广西辅助... 第9分钟必备!闲来山水广西辅助,四川熊猫辅助软件下载(真是真的有挂)-哔哩哔哩四川熊猫辅助软件下载辅...
第4分钟外挂!游戏茶苑辅助器,... 第4分钟外挂!游戏茶苑辅助器,雀友会广东潮汕bus,高科技教程(有挂讲解)-哔哩哔哩在进入雀友会广东...
一分钟普及!小程序牵手跑得有开... 一分钟普及!小程序牵手跑得有开挂吗,闲逸辅助脚本(一直存在有挂)-哔哩哔哩1)小程序牵手跑得有开挂吗...
第8分钟外挂!爱来510k辅助... 第8分钟外挂!爱来510k辅助器,开心泉州辅助,微扑克教程(今日头条)-哔哩哔哩运爱来510k辅助器...