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

相关内容

热门资讯

透视ai!四川长牌皮皮游戏挂,... 透视ai!四川长牌皮皮游戏挂,wepoker安装教程(辅助挂)详细开挂辅助工具 了解更多开挂安装加(...
透视了解!潮汕掌上娱乐脚本,h... 透视了解!潮汕掌上娱乐脚本,hhpoker智能辅助插件(辅助挂)详细开挂辅助神器这是一款可以让一直输...
透明神器!德扑HHpoker有... 透明神器!德扑HHpoker有挂吗,wepoker祈福有用吗(辅助挂)详细开挂辅助安装;无需打开直接...
透视好友!方片十三张透视脚本,... 您好:wpk作弊最怕三个东西这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用...
透视了解!蘑菇云辅助使用视频,... 透视了解!蘑菇云辅助使用视频,we poker游戏下(辅助挂)详细开挂辅助下载您好:蘑菇云辅助使用视...
透视计算!wepoker私人局... 透视计算!wepoker私人局可以透视,hhpoker辅助软件下载(辅助挂)详细开挂辅助安装;无需打...
透视模拟器!微信微乐小程序辅助... 您好:微信微乐小程序辅助器免费安装这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现...
透明规律!超级三加一视频,德普... 透明规律!超级三加一视频,德普之星透视(辅助挂)详细开挂辅助挂;无需打开直接搜索加(薇:136704...
透明教程!wepoker黑侠辅... 透明教程!wepoker黑侠辅助器,pokemmo手机脚本辅助器(辅助挂)详细开挂辅助软件1、下载安...
透视真的!皮皮辅助软件,拱趴大... 透视真的!皮皮辅助软件,拱趴大菠萝怎么开挂(辅助挂)详细开挂辅助安装;无需打开直接搜索加(薇:136...