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

相关内容

热门资讯

推荐十款!约局吧德州有挂吗,拱... 推荐十款!约局吧德州有挂吗,拱趴大菠萝万能挂图解,技巧教程(有挂神器)科技教程也叫必备教程,这是一款...
玩家必备科普!wepoker有... 玩家必备科普!wepoker有没有挂,wpk私人辅助,科技教程(有挂攻略)是一款可以让一直输的玩家,...
攻略讲解!pokermaste... 攻略讲解!pokermaster辅助器,德州透视是真的假的,wpk教程(有挂软件);原来确实真的有挂...
科技新动态!约局吧透视挂下载,... 《科技新动态!约局吧透视挂下载,wepoker怎么破解游戏,必备教程(有挂软件)》 wepoker怎...
热点推荐!pokemmo免费脚... 热点推荐!pokemmo免费脚本,HH平台挂,安装教程(有挂透明)这是由厦门游乐互动科技有限公司精心...
重大通报!newpoker脚本... 1、重大通报!newpoker脚本,wepoker插件功能辅助器,曝光教程(有挂教程);详细教程。2...
推荐十款!wepoker有脚本... 推荐十款!wepoker有脚本吗,wepoker私人局透视插件,存在挂教程(有挂攻略);1分钟了解详...
盘点一款!aapoker俱乐部... 1、盘点一款!aapoker俱乐部靠谱吗,拱趴大菠萝有挂吗,科技教程(有挂辅助);详细教程。2、aa...
来一盘!wepoker私人局俱... 1、来一盘!wepoker私人局俱乐部辅助,impoker辅助,2025教程(有挂软件)。2、wep...
重大来袭!we poker辅助... 这是一款非常优秀的aapoker透视脚本入口 ia辅助检测软件,能够让你了解到aapoker透视脚本...