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、来打麻将透视辅助软件激活码...
玩家必看分享作弊器插件!乐暴延... 玩家必看分享作弊器插件!乐暴延边麻将多功能作弊器插件,乐暴延边麻将一直是真的有作弊器插件运乐暴延边麻...
重大通报外挂脚本!KELE多功... 重大通报外挂脚本!KELE多功能外挂脚本,KELE一直是有外挂脚本1、KELE辅助器安装包、KELE...
发现玩家透视挂软件!随便字牌多... 发现玩家透视挂软件!随便字牌多功能透视挂软件,随便字牌切实是真的有透视挂软件1、首先打开随便字牌辅助...
一分钟揭秘透视器教程!铃铛游戏... 一分钟揭秘透视器教程!铃铛游戏3D多功能透视器教程,铃铛游戏3D一直是真的有透视器教程1、铃铛游戏3...
终于知道作弊器方法!大唐麻将全... 终于知道作弊器方法!大唐麻将全集多功能作弊器方法,大唐麻将全集一直真的是有作弊器方法1、金币登录送、...
我来向大家传授辅助器app!乐... 我来向大家传授辅助器app!乐通多功能辅助器app,乐通一直是真的有辅助器app1、乐通透视辅助软件...
科技介绍透视挂攻略!欢乐赣游G... 科技介绍透视挂攻略!欢乐赣游GZ多功能透视挂攻略,欢乐赣游GZ原来真的是有透视挂攻略1、用户打开应用...
必备攻略透视器脚本!打两圈泰州... 必备攻略透视器脚本!打两圈泰州麻将多功能透视器脚本,打两圈泰州麻将一贯有透视器脚本必备攻略透视器脚本...