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

相关内容

热门资讯

第8分钟辅助!wepoker怎... 第8分钟辅助!wepoker怎么开辅助,黑侠破解wepoker,课程教程(今日头条)1、上手简单,内...
第十分钟辅助!如何下载wpk透... 第十分钟辅助!如何下载wpk透视版,pokerworld软件,策略教程(有挂分享)如何下载wpk透视...
九分钟辅助!wepoker辅助... 九分钟辅助!wepoker辅助真的假的,cloudpoker作弊,手筋教程(证实有挂)1、每一步都需...
第十分钟辅助!wejoker辅... 第十分钟辅助!wejoker辅助器怎么卖,wepoker免费透视脚本,窍门教程(有挂方式)1、进入游...
十分钟辅助!wepoker-h... 十分钟辅助!wepoker-h5下载,pokernow辅助控制,绝活教程(有挂实锤)1、点击下载安装...
5分钟辅助!aapoker辅助... 5分钟辅助!aapoker辅助器怎么用,wepoker模拟器哪个好用,模板教程(有挂辅助)wepok...
八分钟辅助!wpk官网下载链接... 八分钟辅助!wpk官网下载链接,wpk透视怎么安装,教程书教程(有挂分享)该软件可以轻松地帮助玩家将...
9分钟辅助!wepoker怎么... 9分钟辅助!wepoker怎么买辅助,wepoker安装教程,课程教程(有挂方略)1、wepoker...
第七分钟辅助!hh poker... 第七分钟辅助!hh poker插件下载,wepoker科技辅助器,教程书教程(有挂教程)wepoke...
三分钟辅助!hhpoker脚本... 三分钟辅助!hhpoker脚本下载,hhpoker辅助器,举措教程(有挂教程)三分钟辅助!hhpok...