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、乐享麻将连连看辅助免费辅助多个强度级别...
今日重大通报!福建天庭啊开心辅... 今日重大通报!福建天庭啊开心辅助软件,开心泉州小程序工具,详细教程(竟然有挂)1)福建天庭啊开心辅助...
一起来讨论!大唐撸麻雀作弊码,... 一起来讨论!大唐撸麻雀作弊码,潮汕汇游戏辅助,详细软件(真的有挂)1、完成大唐撸麻雀作弊码有辅助插件...
玩家必看科普!途游游戏辅助脚本... 玩家必看科普!途游游戏辅助脚本,欢乐休闲划水辅助,详细插件(有挂方法)途游游戏辅助脚本破解侠是真的助...
信息共享!广东雀神挂件去那买,... 信息共享!广东雀神挂件去那买,花花生活圈开挂,详细脚本(果真有挂)暗藏猫腻,小编详细说明广东雀神挂件...
盘点一款!天天辅助工具,丽水都... 盘点一款!天天辅助工具,丽水都来大菠萝脚本辅助,详细插件(有挂规律)1、金币登录送、破产送、升级送、...
教程辅助!微乐小程序辅助免费,... 教程辅助!微乐小程序辅助免费,小程序家乡大贰脚本,详细挂(竟然有挂)1、这是跨平台的小程序家乡大贰脚...
科技分享!上饶辅助设备出租,河... 科技分享!上饶辅助设备出租,河南微乐小程序辅助器免费,详细插件(竟然有挂)河南微乐小程序辅助器免费辅...
揭秘一下!威信茶馆app插件,... 揭秘一下!威信茶馆app插件,科技实测情怀辅助,详细攻略(果真有挂)一、科技实测情怀辅助可以开透视的...
一分钟秒懂!微乐小程序授权,蜀... 一分钟秒懂!微乐小程序授权,蜀山四川好友房可以开挂吗,详细攻略(有挂透明挂)1、游戏颠覆性的策略玩法...