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

相关内容

热门资讯

透视肯定!wepoker怎么买... 透视肯定!wepoker怎么买辅助,火神大厅辅助(一贯是有挂);1、很好的工具软件,可以解锁游戏的火...
透视科技!aapoker辅助器... 透视科技!aapoker辅助器怎么用,约战武汉辅助插件(一贯是有挂);1)约战武汉辅助插件辅助挂:进...
透视科技!wejoker手机版... 透视科技!wejoker手机版透视脚本,填大坑辅助器(好像是真的有挂)1、首先打开填大坑辅助器最新版...
透视美元局!wpk私人辅助,联... 透视美元局!wpk私人辅助,联盟辅助软件(真是有挂)1、首先打开联盟辅助软件最新版本,在联盟辅助软件...
透视私人局!哈糖大菠萝万能挂,... 透视私人局!哈糖大菠萝万能挂,约局吧可以看到别人的底牌(一直是有挂);1、打开软件启动之后找到中间准...
透视代打!wejoker辅助脚... 透视代打!wejoker辅助脚本,乐乐川南字牌辅助器(其实存在有挂);暗藏猫腻,小编详细说明乐乐川南...
透视教程!wpk私人局辅助是真... 透视教程!wpk私人局辅助是真的吗,财神十三张安装工具(竟然真的是有挂)1、玩家可以在财神十三张安装...
透视规律!xpoker辅助工具... 透视规律!xpoker辅助工具,掌酷十三张辅助器脚本(原来存在有挂);小薇(透视辅助)致您一封信;亲...
透视最新!wpk辅助器,大菠萝... 透视最新!wpk辅助器,大菠萝游戏佛手在线辅助怎么用(切实是真的有挂);1、大菠萝游戏佛手在线辅助怎...
透视软件!wepoker辅助器... 透视软件!wepoker辅助器怎么弄,蘑菇云辅助(原来是有挂)1、完成wepoker辅助器的残局,帮...