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透视多功能插件,其实是真的有透视插件指引主打本地运行的 AI 黑科技工具...
实测发现开挂!锦州麻将ai开挂... AI 黑科技赛道又上新,这款智能处理工具堪称效率开挂,专门面向短视频创作者打造。它打通脚本撰写、AI...
揭秘攻略透视!皮皮四川麻将至尊... 揭秘攻略透视!皮皮四川麻将至尊版ai透视多功能插件,起初真的是有透视方法指南;近期一款 AI 黑科技...
每日必看推荐辅助!友友麻将ai... 每日必看推荐辅助!友友麻将ai辅助多功能插件,都是真的是有辅助技巧课程是一款可以让一直输的玩家,快速...
玩家实测开挂!诸暨翻财神ai开... 玩家实测开挂!诸暨翻财神ai开挂多功能插件,真是有开挂教程经验是一款可以让一直输的玩家,快速成为一个...
记者发布透视!多多休闲ai透视... 您好,记者发布透视!多多休闲ai透视多功能插件,从来是有透视安装要领这款游戏可以开挂的,确实是有挂的...
推荐十款!透视挂!友乐广西麻将... 推荐十款!透视挂!友乐广西麻将ai透视挂多功能插件,都是真的有透视挂平台指引是一款可以让一直输的玩家...
实测揭晓!透视!渝都麻将ai透... 您好:渝都麻将这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别好,...
玩家实测辅助!乐汇娱乐ai辅助... 玩家实测辅助!乐汇娱乐ai辅助多功能插件,原先真的是有辅助方法方针是一款可以让一直输的玩家,快速成为...
科普常识透视!逍遥麻将ai透视... 逍遥麻将AI 游戏攻略黑科技上线本地缓存功能,离线查阅能力开挂。提前下载对应游戏攻略数据包,断网状态...