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、完成微乐手游脚本辅助有辅助插件,...
第二分钟辅助!微乐小程序辅助脚... 第二分钟辅助!微乐小程序辅助脚本,竟然真的是有辅助工具(有挂分享)微乐小程序辅助脚本透视方法中分为三...
8分钟辅助!相约互娱辅助,真是... 8分钟辅助!相约互娱辅助,真是是真的有辅助app(有挂教学)1、玩家可以在相约互娱辅助透视最简单三个...
第三分钟辅助!黑桃k大厅辅助,... 第三分钟辅助!黑桃k大厅辅助,总是有辅助神器(有挂秘笈)1、进入游戏-大厅左侧-新手福利-激活码辅助...
第二分钟辅助!中至上饶打炸漏洞... 第二分钟辅助!中至上饶打炸漏洞,真是是有辅助插件(了解有挂)1、中至上饶打炸漏洞模拟器是什么优化,中...
两分钟辅助!温州茶苑破解版,好... 您好,温州茶苑破解版这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家...
1分钟辅助!禅游指尖四川修改器... 1分钟辅助!禅游指尖四川修改器,果然有辅助方法(有挂总结)1、这是跨平台的禅游指尖四川修改器轻量版有...
第一分钟辅助!情怀西游辅助,好... 第一分钟辅助!情怀西游辅助,好像是有辅助教程(有挂方法)1、全新机制【情怀西游辅助ai辅助工具激活码...
第十分钟辅助!新老夫子脚本,果... 第十分钟辅助!新老夫子脚本,果然是真的有辅助工具(有挂规律)1.新老夫子脚本 选牌创建新账号,点击进...