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、下载好禾城麻将脚本下载之后...
玩家必看科普开挂!西兵互娱辅助... 玩家必看科普开挂!西兵互娱辅助透视挂,天星是有外挂,一直有人有挂1)西兵互娱免费钻石:进一步探索西兵...
如何分辨真伪开挂!新葡京辅助透... 如何分辨真伪开挂!新葡京辅助透视挂,海商游戏是有外挂,竟然确实有挂所有人都在同一条线上,像星星一样排...
信息共享开挂!涡阳茶馆辅助透视... 信息共享开挂!涡阳茶馆辅助透视挂,情怀河北麻将存在有外挂,果然有挂技术1、涡阳茶馆有没有辅助教程、涡...
据了解开挂!新蛮王辅助透视挂,... 据了解开挂!新蛮王辅助透视挂,莲花麻将是有外挂,都是真是有挂1)新蛮王辅助插件:进一步探索新蛮王辅助...
今日科普透视挂!吉利比鸡辅助透... 今日科普透视挂!吉利比鸡辅助透视挂,心动休闲是真的有外挂,本来有挂工具1、全新机制【吉利比鸡ai辅助...
一分钟了解辅助!新九五辅助透视... 一分钟了解辅助!新九五辅助透视挂,微乐云南麻将确实有外挂,竟然果真有挂该软件可以轻松地帮助玩家将微乐...
据了解开挂!青云阁辅助透视挂,... 据了解开挂!青云阁辅助透视挂,闽南漳州麻将存在有外挂,好像有挂实锤1、这是跨平台的青云阁轻量版有透视...
一分钟揭秘开挂!优乐扑克辅助透... 一分钟揭秘开挂!优乐扑克辅助透视挂,鼎圣麻将真的有外挂,竟然有挂方式小薇(辅助器软件下载)致您一封信...