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

相关内容

热门资讯

今日科普!欢乐茶馆脚本辅助,w... 今日科普!欢乐茶馆脚本辅助,wepoker辅助插件功能,我来教教你(有挂方针);欢乐茶馆脚本辅助AI...
推荐攻略!潮汕激k辅助器,如何... 推荐攻略!潮汕激k辅助器,如何下载wpk透视版,插件教程(真实有挂)是一款可以让一直输的玩家,快速成...
重大通报!仟众部落辅助,拱趴大... 重大通报!仟众部落辅助,拱趴大菠萝有什么挂,可靠教程(有挂教学);仟众部落辅助软件透明挂作为一款仟众...
盘点一款!打两圈辅助,wepo... 盘点一款!打两圈辅助,wepoker辅助透视,透牌教程(有挂秘籍);打两圈辅助最新版本免费下载安装哦...
必看攻略!悟空大厅微信辅助,h... 必看攻略!悟空大厅微信辅助,hhpoker有作弊的吗,揭秘攻略(有挂分析);悟空大厅微信辅助AI智能...
一分钟揭秘!牵手跑得软件,we... 您好:牵手跑得软件这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别...
攻略讲解!皮皮游戏辅助平台,w... 攻略讲解!皮皮游戏辅助平台,wepoker破解是真的还是假的,攻略方法(有挂解惑);皮皮游戏辅助平台...
分辨真假!家乡大二有没有辅助,... 《分辨真假!家乡大二有没有辅助,来玩app破解,揭秘教程(有挂实锤)》 家乡大二有没有辅助软件透明挂...
攻略讲解!广西老友玩有破解码,... 攻略讲解!广西老友玩有破解码,来玩app破解版,玩家教你(有挂辅助);小薇(透视辅助)致您一封信;亲...
记者发布!孝感卡五星辅助,we... 记者发布!孝感卡五星辅助,wepoker有透视吗,2025新版教程(竟然有挂);孝感卡五星辅助软件透...