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

你

相关内容

热门资讯

热门推荐作弊器脚本!边锋cr在... 热门推荐作弊器脚本!边锋cr在线多功能作弊器脚本,边锋cr在线竟然存在有作弊器脚本热门推荐作弊器脚本...
必备教程透视挂方法!澳门永利集... 必备教程透视挂方法!澳门永利集团多功能透视挂方法,澳门永利集团其实是真的有透视挂方法1、许多玩家不知...
查到实测辅助辅助器工具!抚顺麻... 查到实测辅助辅助器工具!抚顺麻将多功能辅助器工具,抚顺麻将一贯是有辅助器工具1)抚顺麻将辅助插件:进...
科技通报透视器脚本!赫麒众游多... 科技通报透视器脚本!赫麒众游多功能透视器脚本,赫麒众游确实有透视器脚本;在进入赫麒众游软件靠谱后,参...
专业讨论作弊器方法!海迅麻将多... 专业讨论作弊器方法!海迅麻将多功能作弊器方法,海迅麻将其实是真的有作弊器方法暗藏猫腻,小编详细说明海...
热点讨论作弊器教程!宁夏麻将多... 热点讨论作弊器教程!宁夏麻将多功能作弊器教程,宁夏麻将都是真的有作弊器教程1、进入游戏-大厅左侧-新...
如何分辨真伪辅助器软件!小闲巴... 如何分辨真伪辅助器软件!小闲巴渝棋牌多功能辅助器软件,小闲巴渝棋牌一贯存在有辅助器软件所有人都在同一...
让我来分享经验透视器app!老... 让我来分享经验透视器app!老友汇多功能透视器app,老友汇总是是真的有透视器app运老友汇辅助工具...
科技揭秘开挂攻略!指尖四川麻将... 科技揭秘开挂攻略!指尖四川麻将多功能开挂攻略,指尖四川麻将确实是有开挂攻略指尖四川麻将脚本下载中分为...
一秒答解透视器技巧!福建众娱多... 一秒答解透视器技巧!福建众娱多功能透视器技巧,福建众娱都是真的是有透视器技巧1、点击下载安装,福建众...