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

相关内容

热门资讯

透视能赢"wepok... 透视能赢"wepoker怎么开辅助"详细辅助辅助教程(可以发冤家牌软件);wepoker怎么开辅助软...
透视app"poke... 透视app"pokerworld修改器"详细辅助详细教程(辅助器免费教程);pokerworld修改...
透视透视"智星菠萝可... 透视透视"智星菠萝可以辅助吗"详细辅助解密教程(辅助器下载方法)1、玩家可以在智星菠萝可以辅助吗软件...
辅助透视"约局吧辅助... 辅助透视"约局吧辅助器"详细辅助透明教程(到底有透视软件);《WPK辅助透视》‌:支持手机实时对战,...
透视新版"德普之星私... 您好,德普之星私人局辅助免费这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多...
透视苹果版"德普之星... 透视苹果版"德普之星透视辅助插件"详细辅助透明挂教程(看底牌透视);大神普及一款德州ai内幕,确定了...
透视神器"hardr... 透视神器"hardrock作弊"详细辅助透视教程(有透视功能方法);1.hardrock作弊 ai辅...
透视实锤"wepok... 透视实锤"wepoker插件功能辅助器"详细辅助曝光教程(有人用过透明);科技安装教程;136704...
透视辅助"wepok... 您好,wepoker透视辅助下载这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】...
透视辅助"wepok... 1、透视辅助"wepoker插件下载"详细辅助攻略教程(轻量版书签技巧);详细教程。2、wepoke...