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

相关内容

热门资讯

透视脚本!wpk德州局怎么透视... 透视脚本!wpk德州局怎么透视,果然是真的有挂(透视)力荐教程(有挂解说)进入游戏-大厅左侧-新手福...
透视挂透视!wpk俱乐部有没有... 透视挂透视!wpk俱乐部有没有辅助,wpk辅助,普及教程(一贯是真的有挂);一、wpk俱乐部有没有辅...
透视线上!pokemmo辅助官... 透视线上!pokemmo辅助官网,pokerrrr2辅助,微扑克教程(有挂方法)1、进入到pokem...
透视安卓版!wepoker数据... 透视安卓版!wepoker数据分析工具,确实是真的有挂(透视)透视教程(有挂插件);1、这是跨平台的...
透视辅助!wpk辅助是什么,w... 透视辅助!wpk辅助是什么,wpk透视辅助方法,安装教程(本来有挂)1、许多玩家不知道wpk透视辅助...
透视免费!拱趴大菠萝万能辅助器... 透视免费!拱趴大菠萝万能辅助器,poker master辅助,玩家教你(有挂介绍);1)拱趴大菠萝万...
透视科技!xpoker辅助神器... 透视科技!xpoker辅助神器,先前真的有挂(透视)介绍教程(有挂教程)1、打开软件启动之后找到中间...
透视辅助!wpk软件是真的吗,... 透视辅助!wpk软件是真的吗,wpk显示有作弊,黑科技教程(原来存在有挂)1、透视辅助!wpk软件是...
透视好友房!fishpoker... 透视好友房!fishpoker透视底牌,哈糖大菠萝万能挂,可靠教程(有挂黑科技)1、任何哈糖大菠萝万...
透视真的!werplan免费挂... 透视真的!werplan免费挂下载,竟然真的有挂(透视)攻略教程(有挂细节)1、每一步都需要思考,不...