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

相关内容

热门资讯

推荐十款!开心网辅助工具,乾坤... 推荐十款!开心网辅助工具,乾坤互娱辅助,必备教程(有挂猫腻)-哔哩哔哩;开心网辅助工具是一种具有地方...
今日焦点!天天卡五星辅助器,7... 今日焦点!天天卡五星辅助器,728游戏怎么控制的,切实教程(竟然有挂)-哔哩哔哩;致您一封信;亲爱天...
玩家攻略推荐!边锋小程序老友辅... 玩家攻略推荐!边锋小程序老友辅助,福建天天开心辅助工具,AI教程(今日头条)-哔哩哔哩;福建天天开心...
盘点一款!情怀游戏字牌辅助,葫... 盘点一款!情怀游戏字牌辅助,葫芦娃七子降妖攻略,解密教程(有挂辅助)-哔哩哔哩;葫芦娃七子降妖攻略软...
一分钟了解!财神十三章有哪些辅... 一分钟了解!财神十三章有哪些辅助功能,创思维激k看底牌辅助,安装教程(确实有挂)-哔哩哔哩;一、财神...
透视神器!小程序微乐辅助软件,... 透视神器!小程序微乐辅助软件,新鸿狐辅助软件是真的吗,详细教程(了解有挂)-哔哩哔哩;新鸿狐辅助软件...
透视玄学!兴动助手辅助器,好友... 透视玄学!兴动助手辅助器,好友赣南能装挂吗,靠谱教程(有挂实锤)-哔哩哔哩;详细好友赣南能装挂吗攻略...
透视挂!打大a开挂神器免费下载... 透视挂!打大a开挂神器免费下载,皮皮胡子辅助,透明挂教程(竟然有挂)-哔哩哔哩;小薇(透视辅助)致您...
发现一款!掌中乐游戏中心破解版... 发现一款!掌中乐游戏中心破解版,微乐家乡破解,实用技巧(有挂规律)-哔哩哔哩;1、不需要AI权限,帮...
总算了解!欢乐对决辅助菜单,榕... 总算了解!欢乐对决辅助菜单,榕城510k有没有挂,技巧教程(揭秘有挂)-哔哩哔哩;1、超多福利:超高...