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

你

相关内容

热门资讯

玩家交流!边锋斗地主辅助功能神... 玩家交流!边锋斗地主辅助功能神器,边锋斗地主其实有辅助神器1、许多玩家不知道边锋斗地主辅助怎么退出观...
每日必看!红豆大厅辅助功能插件... 每日必看!红豆大厅辅助功能插件,红豆大厅一直真的有辅助插件1、在红豆大厅插件功能辅助器技巧中,中转单...
普及知识!艺龙游戏辅助功能攻略... 普及知识!艺龙游戏辅助功能攻略,艺龙游戏总是真的是有辅助攻略1、不需要AI权限,帮助你快速的进行艺龙...
实测揭晓!爱上斗牌开挂功能软件... 实测揭晓!爱上斗牌开挂功能软件,爱上斗牌果然真的是有开挂软件1、这是跨平台的爱上斗牌轻量版有透视,在...
免费测试版!边锋老友宁安麻将辅... 免费测试版!边锋老友宁安麻将辅助器功能app,边锋老友宁安麻将一贯存在有辅助器app1、起透看视 边...
避坑细节!白金岛湘潭三打哈辅助... 避坑细节!白金岛湘潭三打哈辅助器功能神器,白金岛湘潭三打哈确实存在有辅助器神器1、首先打开白金岛湘潭...
指导大家!牌九辅助功能教程,牌... 指导大家!牌九辅助功能教程,牌九总是真的有辅助教程1、指导大家!牌九辅助功能教程,牌九总是真的有辅助...
玩家必看科普!中至永丰麻将开挂... 玩家必看科普!中至永丰麻将开挂功能插件,中至永丰麻将原来真的是有开挂插件1、很好的工具软件,可以解锁...
实测发现!吕梁打七透视挂功能技... 实测发现!吕梁打七透视挂功能技巧,吕梁打七其实是有透视挂技巧1、首先打开吕梁打七辅助器下载最新版本,...
必备攻略!四川游戏家园透视功能... 必备攻略!四川游戏家园透视功能工具,四川游戏家园一贯真的是有透视工具1)四川游戏家园免费钻石:进一步...