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作弊码,xpoker辅助控制,切实真的有挂(新版有挂)1)免费钻石:进一步探...
今天上午!哈糖大菠萝挂,德扑之... 今天上午!哈糖大菠萝挂,德扑之星透视辅助插件,其实是有挂(有挂细节)1、游戏颠覆性的策略玩法,独创攻...
最强版we-poker靠谱吗,... 最强版we-poker靠谱吗,wepoker私人局俱乐部辅助,真是是有挂(的确有挂)亲,关键说明,透...
最强版wpk官网下载链接,we... 最强版wpk官网下载链接,wepoker辅助透视软件,一贯真的是有挂(有挂技巧)1、下载好wepok...
黑科技辅助挂!wepoker透... 黑科技辅助挂!wepoker透视脚本免费,德普之星透视辅助软件是真的吗,一直存在有挂(有挂存在)1、...
最强版哈糖大菠萝助手,poke... 最强版哈糖大菠萝助手,poker world辅助,本来真的是有挂(详细教程)运辅助工具,进入游戏界面...
2026版教程!sohoo开挂... 2026版教程!sohoo开挂辅助,智星德州辅助译码插件靠谱吗,一直是真的有挂(有挂辅助)智星德州辅...
最强版购买的wpk辅助在哪里下... 最强版购买的wpk辅助在哪里下载,德州真人透视脚本,竟然有挂(有挂方针)1、许多玩家不知道辅助怎么退...
规律辅助挂!佛手在线是不是有挂... 规律辅助挂!佛手在线是不是有挂,德扑之星透视软件免费入口官网,一直确实有挂(有挂详情)1、下载好德扑...
最强版wepoker私人局可以... 最强版wepoker私人局可以透视,sohoo竞技联盟辅助器,切实是真的有挂(有挂攻略)1、该软件可...