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

相关内容

热门资讯

外挂攻略!红龙poker辅助,... 外挂攻略!红龙poker辅助,德普之星透视-真是有辅助脚本(哔哩哔哩)1、红龙poker辅助公共底牌...
外挂妙招!uupoker透视,... 外挂妙招!uupoker透视,sohoo开挂辅助-确实有辅助方法(哔哩哔哩);sohoo开挂辅助破解...
外挂项目!佛手在线大菠萝技巧,... 外挂项目!佛手在线大菠萝技巧,xpoker辅助器-本来存在有辅助工具(哔哩哔哩)1、打开软件启动之后...
外挂窍门!德普之星透视辅助软件... 外挂窍门!德普之星透视辅助软件,德扑圈有透视吗-一直是有辅助app(哔哩哔哩)1、让任何用户在无需安...
外挂妙计!菠萝德普辅助器免费版... 外挂妙计!菠萝德普辅助器免费版在哪里,德扑之心免费透视-其实真的有辅助教程(哔哩哔哩)1、菠萝德普辅...
外挂手段!约局吧德州有挂吗,哈... 外挂手段!约局吧德州有挂吗,哈糖大菠萝万能挂-原来有辅助神器(哔哩哔哩)该软件可以轻松地帮助玩家将约...
外挂妙计!德普之星透视免费,大... 外挂妙计!德普之星透视免费,大菠萝免费辅助器-一直是有辅助app(哔哩哔哩)1、许多玩家不知道大菠萝...
外挂绝活!福建大菠萝万能辅助器... 外挂绝活!福建大菠萝万能辅助器,pokemomo辅助工具-一直是真的有辅助神器(哔哩哔哩)在进入福建...
外挂操作!德普之星透视,智星德... 外挂操作!德普之星透视,智星德州插件怎么下载-一贯是有辅助插件(哔哩哔哩)暗藏猫腻,小编详细说明智星...
外挂积累!来玩app 德州 辅... 外挂积累!来玩app 德州 辅助,德普之星的辅助工具介绍-其实是有辅助技巧(哔哩哔哩)1、这是跨平台...