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

你

相关内容

热门资讯

重大通报辅助!梦起明月辅助透视... 重大通报辅助!梦起明月辅助透视挂,小闲巴渝棋牌真的有外挂,一直真是有挂1、起透看视 小闲巴渝棋牌辅助...
实测分享透视挂!长乐互娱辅助透... 实测分享透视挂!长乐互娱辅助透视挂,新天天游戏是有外挂,果然有挂方式1、金币登录送、破产送、升级送、...
科普分享透视挂!天天休闲辅助透... 科普分享透视挂!天天休闲辅助透视挂,欢乐南通长牌存在有外挂,其实证实有挂1、用户打开应用后不用登录就...
随着透视挂!滇麻圈辅助透视挂,... 随着透视挂!滇麻圈辅助透视挂,陕麻圈是有外挂,总是有挂助手1、陕麻圈公共底牌简单,陕麻圈透视插件操作...
科技通报透视挂!榆林打锅子辅助... 科技通报透视挂!榆林打锅子辅助透视挂,怀远麻将确实有外挂,果然有挂方法;1、榆林打锅子公共底牌简单,...
重大推荐辅助!风风跑胡子辅助透... 重大推荐辅助!风风跑胡子辅助透视挂,海迅麻将真的有外挂,其实有挂教程1、下载好风风跑胡子脚本下载之后...
网友热议透视挂!同城游监利开机... 网友热议透视挂!同城游监利开机辅助透视挂,随便字牌确实有外挂,一直有挂教程1、同城游监利开机脚本辅助...
必看攻略开挂!星脉777辅助透... 必看攻略开挂!星脉777辅助透视挂,来来台安麻将确实有外挂,确实有挂教程1、进入游戏-大厅左侧-新手...
科技新动态透视挂!天龙座大厅辅... 科技新动态透视挂!天龙座大厅辅助透视挂,天天黄石麻将是有外挂,确实有挂辅助1、打开软件启动之后找到中...
来临辅助!新九五辅助透视挂,打... 来临辅助!新九五辅助透视挂,打牌网真的是有外挂,其实有挂分享1、每一步都需要思考,不同水平的挑战新九...