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、超多福利:超高返利,海量正版...
专业透视!pokerworld... 专业透视!pokerworld破解版下载!真是一直总是有辅助软件(有挂秘诀)-哔哩哔哩1、让任何用户...
解密透视!德州真人透视脚本!果... 解密透视!德州真人透视脚本!果然有辅助app(有挂细节)-哔哩哔哩1、任何德州真人透视脚本透视是真的...
辅助透视!hardrock透视... 辅助透视!hardrock透视工具!其实真的是有辅助脚本(有挂秘笈)-哔哩哔哩1、金币登录送、破产送...
解密透视!pokermaste... 解密透视!pokermaster辅助器!果然真的是有辅助工具(有挂讲解)-哔哩哔哩1、pokerma...
必备透视!拱趴大菠萝有什么挂!... 必备透视!拱趴大菠萝有什么挂!果然真的是有辅助工具(有挂透视)-哔哩哔哩1、首先打开拱趴大菠萝有什么...
推荐透视!约局吧德州透视!确实... 推荐透视!约局吧德州透视!确实一直都是有辅助攻略(的确有挂)-哔哩哔哩1、约局吧德州透视辅助软件下载...
必备透视!拱趴大菠萝有什么挂!... 必备透视!拱趴大菠萝有什么挂!一直一直总是有辅助软件(有挂方式)-哔哩哔哩1)拱趴大菠萝有什么挂辅助...
科普透视!xpoker辅助工具... 科普透视!xpoker辅助工具!一贯是真的有辅助神器(确实有挂)-哔哩哔哩一、xpoker辅助工具游...
科普透视!德州真人透视脚本!本... 科普透视!德州真人透视脚本!本来存在有辅助技巧(有挂攻略)-哔哩哔哩德州真人透视脚本能透视中分为三种...