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、打开软件启动之后找到中间准星...
技术分享!丰城麻将软件下载(透... 技术分享!丰城麻将软件下载(透视挂),丰城麻将果然有透视挂app1、下载好丰城麻将透视辅助下载之后点...
科技通报!微乐福建麻将可以装挂... 科技通报!微乐福建麻将可以装挂吗(透视挂),微乐福建麻将竟然真的有透视挂方式1、让任何用户在无需微乐...
玩家必看科普!洛阳麻将透视挂(... 玩家必看科普!洛阳麻将透视挂(透视挂),洛阳麻将本来存在有透视挂演示1、洛阳麻将破解器简单,洛阳麻将...
科技新动态!长城麻将外卦神器软... 科技新动态!长城麻将外卦神器软件(透视挂),长城麻将果然是有透视挂软件1、操作简单,无需长城麻将手机...
新手必备!明珠可以装挂吗(辅助... 新手必备!明珠可以装挂吗(辅助挂),明珠果然真的有辅助挂烘培1、金币登录送、破产送、升级送、活动送。...
发现玩家!中州棋牌辅助软件(开... 发现玩家!中州棋牌辅助软件(开挂),中州棋牌一直有开挂经验发现玩家!中州棋牌辅助软件(开挂),中州棋...
终于清楚!花城牌舍怎么开挂(透... 终于清楚!花城牌舍怎么开挂(透视),花城牌舍一贯存在有透视指引1、花城牌舍公共底牌简单,花城牌舍透视...