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、操作简单,无...
第一分钟外挂!财神十三张脚本效... 第一分钟外挂!财神十三张脚本效果图,wepoker破解版内购,AI教程(有挂教程)-哔哩哔哩1、上手...
7分钟外挂!巴郎新疆开挂,黑桃... 7分钟外挂!巴郎新疆开挂,黑桃a3辅助,解说技巧(确实有挂)-哔哩哔哩;1、下载好巴郎新疆开挂辅助软...
第1分钟外挂!小程序牵手跑得有... 第1分钟外挂!小程序牵手跑得有辅助器,反杀新火神辅助,德州教程(新版有挂)-哔哩哔哩1、游戏颠覆性的...
九分钟外挂!浙江游戏大厅怎么开... 九分钟外挂!浙江游戏大厅怎么开挂,多乐跑得私人房间插件,必胜教程(真的有挂)-哔哩哔哩小薇(透视辅助...
第7分钟外挂!闲来辅助神器,决... 第7分钟外挂!闲来辅助神器,决胜游戏辅助器,详细教程(有挂技术)-哔哩哔哩1、用户打开应用后不用登录...
五分钟外挂!决战血流辅助器,牵... 五分钟外挂!决战血流辅助器,牵手游戏辅助,安装教程(有挂方略)-哔哩哔哩1、每一步都需要思考,不同水...
第6分钟外挂!微信多乐跑辅助,... 第6分钟外挂!微信多乐跑辅助,天天微友有辅助吗,规律教程(确实有挂)-哔哩哔哩1、天天微友有辅助吗系...
第六分钟外挂!南通长牌辅助器插... 第六分钟外挂!南通长牌辅助器插件,八闽福建辅助,攻略方法(有挂秘笈)-哔哩哔哩运南通长牌辅助器插件辅...
第5分钟外挂!四川游戏家园破解... 第5分钟外挂!四川游戏家园破解版,顺欣茶楼开挂,实用技巧(真是有挂)-哔哩哔哩1)四川游戏家园破解版...