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、实时陕西辅助透视辅助更新:用户可以随...
2分钟步骤!土豪辅助(外挂)都... 2分钟步骤!土豪辅助(外挂)都是存在有辅助攻略(哔哩哔哩)1、土豪辅助免费脚本咨询教程、土豪辅助免费...
五分钟积累!微信小程序家乡大贰... 五分钟积累!微信小程序家乡大贰破解版(外挂)原来存在有辅助技巧(哔哩哔哩)1、微信小程序家乡大贰破解...
3分钟技法!嘟咪互动修改器(外... 3分钟技法!嘟咪互动修改器(外挂)本来一直都是有辅助攻略(哔哩哔哩)1、玩家可以在嘟咪互动修改器线上...
第2分钟课程!新道游智能辅助器... 第2分钟课程!新道游智能辅助器(外挂)一直是真的有辅助方法(哔哩哔哩)1、实时新道游智能辅助器透视辅...
第四分钟总结!潮友会app下载... 第四分钟总结!潮友会app下载安卓(外挂)原来一直都是有辅助方法(哔哩哔哩)1、完成潮友会app下载...
3分钟课程!同城游辅助软件(外... 3分钟课程!同城游辅助软件(外挂)切实一直都是有辅助技巧(哔哩哔哩)同城游辅助软件能透视中分为三种模...
九分钟方式!兴动休闲辅助(外挂... 九分钟方式!兴动休闲辅助(外挂)一直是真的有辅助方法(哔哩哔哩)1、金币登录送、破产送、升级送、活动...
第三分钟诀窍!今日长牌辅助(外... 第三分钟诀窍!今日长牌辅助(外挂)总是真的是有辅助技巧(哔哩哔哩)1、下载好今日长牌辅助脚本下载之后...
7分钟秘籍!中至辅助(外挂)本... 7分钟秘籍!中至辅助(外挂)本来真的有辅助app(哔哩哔哩)1、实时中至辅助透视辅助更新:用户可以随...