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

你

相关内容

热门资讯

2024教程!武汉麻将透视软件... 2024教程!武汉麻将透视软件(作弊器),武汉麻将一直是真的有作弊器窍要1、每一步都需要思考,不同水...
玩家实测!琼崖海南麻将开挂技巧... 玩家实测!琼崖海南麻将开挂技巧(透视挂),琼崖海南麻将原来是真的有透视挂诀窍1、很好的工具软件,可以...
记者发布!腾讯广东麻将怎么开挂... 记者发布!腾讯广东麻将怎么开挂(辅助器),腾讯广东麻将一贯有辅助器总结1、腾讯广东麻将免费脚本咨询教...
重大来袭!趣味棋牌到底有没有透... 重大来袭!趣味棋牌到底有没有透视挂(外挂),趣味棋牌其实存在有外挂法子该软件可以轻松地帮助玩家将趣味...
一分钟了解!雀友会广东潮汕麻将... 一分钟了解!雀友会广东潮汕麻将开挂透视教程(辅助挂),雀友会广东潮汕麻将确实是真的有辅助挂秘籍暗藏猫...
记者揭秘!微乐山东麻将哪里有挂... 记者揭秘!微乐山东麻将哪里有挂(开挂),微乐山东麻将原来是有开挂手册1、微乐山东麻将模拟器是什么优化...
分辨真假!玉兔互娱确实有挂(透... 分辨真假!玉兔互娱确实有挂(透视),玉兔互娱一贯真的有透视妙招进入游戏-大厅左侧-新手福利-激活码辅...
实测揭晓!嘟咪互动是不是有挂(... 实测揭晓!嘟咪互动是不是有挂(透视),嘟咪互动确实真的有透视app1、超多福利:超高返利,海量正版游...
重大通报!白金岛麻将圈开挂脚本... 重大通报!白金岛麻将圈开挂脚本(透视挂),白金岛麻将圈真是是真的有透视挂方案1、任何白金岛麻将圈透视...
玩家必备科普!川渝互娱到底有没... 玩家必备科普!川渝互娱到底有没有挂(透视挂),川渝互娱总是真的有透视挂策略1、川渝互娱辅助器安装包、...