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.欢乐五张 选牌创...
今日科普!十三十三水辅助功能a... 今日科普!十三十三水辅助功能app,十三十三水都是真的是有辅助app(有辅助挂教学)1)十三十三水免...
一分钟了解!浙江游戏大厅辅助功... 一分钟了解!浙江游戏大厅辅助功能教程,浙江游戏大厅真是存在有辅助教程(有辅助挂辅教程)1、起透看视 ...
三分钟了解!寻找锦鲤辅助器功能... 三分钟了解!寻找锦鲤辅助器功能技巧,寻找锦鲤一直真的是有辅助器技巧(有辅助挂秘笈)寻找锦鲤是不是有人...
新手必备!亨博棋牌透视挂功能神... 新手必备!亨博棋牌透视挂功能神器,亨博棋牌一贯存在有透视挂神器(有辅助挂分析)1、玩家可以在亨博棋牌...
交流学习经验!福乐游贰柒拾透视... 交流学习经验!福乐游贰柒拾透视挂功能神器,福乐游贰柒拾总是真的是有透视挂神器(有挂辅方法)1、福乐游...
攻略讲解!53开元辅助功能教程... 攻略讲解!53开元辅助功能教程,53开元原来真的是有辅助教程(有辅助挂方法)1、53开元透视辅助软件...
玩家亲测!老友广东棋牌开挂功能... 玩家亲测!老友广东棋牌开挂功能攻略,老友广东棋牌本来是有开挂攻略(有辅助挂猫腻)老友广东棋牌能透视中...
玩家必备科普!天胡开挂功能方法... 玩家必备科普!天胡开挂功能方法,天胡真是是有开挂方法(有辅助挂分享);进入游戏-大厅左侧-新手福利-...