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、情怀西游辅助公共底牌简单,情...
2026版技巧!优优乐暗宝源码... 2026版技巧!优优乐暗宝源码!都是真的是有开挂辅助攻略(发现有挂)-哔哩哔哩优优乐暗宝源码能透视中...
技巧辅助挂!情怀宜春怎么开挂!... 技巧辅助挂!情怀宜春怎么开挂!果然真的有开挂辅助app(确实有挂)-哔哩哔哩1、让任何用户在无需情怀...
于此同时!h5新全游辅助!一贯... 于此同时!h5新全游辅助!一贯是真的有开挂辅助软件(真实有挂)-哔哩哔哩在进入h5新全游辅助软件靠谱...
于此同时!四川途游小程序辅助软... 于此同时!四川途游小程序辅助软件!好像确实有开挂辅助攻略(有挂秘籍)-哔哩哔哩1、超多福利:超高返利...