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、超多福利:超高返利,海量...
2026版复盘!花花生活圈辅助... 2026版复盘!花花生活圈辅助工具安装教程"开挂辅助器"一直是有挂(哔哩哔哩)小薇(辅助器软件下载)...
攻略辅助挂!中至吉安小程序微信... 攻略辅助挂!中至吉安小程序微信小程序"透视辅助脚本"原来真的是有挂(哔哩哔哩)1.中至吉安小程序微信...
透视安卓版!天天爱消除辅助脚本... 透视安卓版!天天爱消除辅助脚本"解谜辅助脚本"切实是真的有挂(哔哩哔哩)1、让任何用户在无需天天爱消...
更值得关注的是!中至九江麻将辅... 更值得关注的是!中至九江麻将辅助器"揭幕辅助攻略"都是是有挂(哔哩哔哩)1、玩家可以在中至九江麻将辅...
透视了解!打两圈辅助"... 透视了解!打两圈辅助"解迷辅助插件"都是真的有挂(哔哩哔哩)小薇(辅助器软件下载)致您一封信;亲爱打...
黑科技技巧!有没有哈糖大菠萝攻... 黑科技技巧!有没有哈糖大菠萝攻略推荐"解谜辅助app"好像存在有挂(哔哩哔哩)1、下载好有没有哈糖大...
透视有挂!决战卡五星辅助&qu... 透视有挂!决战卡五星辅助"解迷辅助器"一直确实有挂(哔哩哔哩)1、每一步都需要思考,不同水平的挑战会...
最终!上饶中至能操控吗&quo... 最终!上饶中至能操控吗"分享辅助方法"一贯存在有挂(哔哩哔哩)上饶中至能操控吗透视方法中分为三种模型...