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权限,帮助...
分享个大家!wepoker数据... 分享个大家!wepoker数据分析工具,pokerrrr2辅助,其实是有挂(真的有挂)1、辅助软件下...
三分钟了解!aapoker怎么... 三分钟了解!aapoker怎么控制牌,哈糖大菠萝开挂,原来有挂(竟然有挂)1、进入到是否有挂之后,能...
专业讨论!德州局hhpoker... 专业讨论!德州局hhpoker,wepoker辅助透视,竟然存在有挂(有挂教程)专业讨论!德州局hh...
科技揭秘!wepoker辅助透... 科技揭秘!wepoker辅助透视软件,菠萝德普辅助器免费版在哪里,本来真的是有挂(有挂教程)科技揭秘...
必备科技!德普之星透视软件免费... 必备科技!德普之星透视软件免费入口官网,wepoker网页版透视方法,其实是真的有挂(有挂方式)1、...
盘点十款!pokemmo辅助器... 盘点十款!pokemmo辅助器,德州透视是真的假的,都是真的有挂(真实有挂)1、起透看视 辅助软件价...
热点讨论!wpk刷入池率脚本,... 热点讨论!wpk刷入池率脚本,约局吧辅助器,一直是有挂(有挂功能)1、超多福利:超高返利,海量正版游...
最新研发!hhpoker有没有... 最新研发!hhpoker有没有作弊辅助,wepoker有没有插件,确实有挂(有挂秘诀)1)有没有挂:...
我来教大家!wejoker私人... 我来教大家!wejoker私人辅助软件,aapoker插件,其实是真的有挂(有挂解密)1、公共底牌简...