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

相关内容

热门资讯

德州之星插件!aapoker可... 德州之星插件!aapoker可以开挂吗(透视)一向真的有挂(详细辅助透明挂教程)(1)德州之星插件!...
透视规律!hhpoker透视脚... 透视规律!hhpoker透视脚本视频,aapoker辅助怎么用(详细辅助必赢方法)1、点击下载安装,...
德扑之星有猫腻!wepower... 德扑之星有猫腻!wepower有外挂吗(透视)一贯是有挂(详细辅助AI教程)1、超多福利:超高返利,...
透视工具!德普之星有没有挂,w... 透视工具!德普之星有没有挂,wepoker辅助透视软件(详细辅助2025教程)wepoker辅助透视...
微扑克辅助挂!wpk微扑克真的... 微扑克辅助挂!wpk微扑克真的有辅助插件吗(透视)果然是有挂(详细辅助力荐教程)是一款可以让一直输的...
透视黑科技!红龙poker脚本... 透视黑科技!红龙poker脚本,有没有人wepoker(详细辅助黑科技教程)1、不需要AI权限,帮助...
德扑ai智能机器人!德扑软件高... 德扑ai智能机器人!德扑软件高端(透视)原本存在有挂(详细辅助2025版教程);是一款可以让一直输的...
透视最新!wepoker免费永... 透视最新!wepoker免费永久脚本,pokemmo内置修改器(详细辅助AI教程)1、全新机制【we...
aapoker外挂!扑克世界辅... aapoker外挂!扑克世界辅助软件(透视)一向是真的有挂(详细辅助总结教程)您好,aapoker外...
透视安卓版!aapoker辅助... 透视安卓版!aapoker辅助插件工具,wpk辅助软件(详细辅助透视教程)1、游戏颠覆性的策略玩法,...