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

相关内容

热门资讯

外挂指引!xpoker辅助怎么... 外挂指引!xpoker辅助怎么用,xpoker辅助控制-果然是有辅助教程(哔哩哔哩)1、任何xpok...
外挂练习!红龙poker有辅助... 外挂练习!红龙poker有辅助吗,约局吧德州真的存在透视吗-竟然是有辅助方法(哔哩哔哩)1、玩家可以...
外挂演示!拱趴大菠萝自动计算机... 外挂演示!拱趴大菠萝自动计算机器人,智星菠萝辅助-切实一直总是有辅助教程(哔哩哔哩)一、拱趴大菠萝自...
外挂模板!德扑之心免费透视,德... 外挂模板!德扑之心免费透视,德州局怎么透视-一贯一直都是有辅助工具(哔哩哔哩)在进入德州局怎么透视软...
外挂模板!wepoker安装教... 外挂模板!wepoker安装教程,线上德州的辅助器是什么-其实存在有辅助教程(哔哩哔哩)暗藏猫腻,小...
外挂要领!sohoo poke... 外挂要领!sohoo poker辅助器,werplan有挂吗-都是一直都是有辅助插件(哔哩哔哩)1)...
外挂指南书!拱趴大菠萝万能辅助... 外挂指南书!拱趴大菠萝万能辅助器,约局吧作必弊脚本-总是有辅助教程(哔哩哔哩)1、拱趴大菠萝万能辅助...
外挂模板!智星菠萝德州插件最新... 外挂模板!智星菠萝德州插件最新版本更新内容详解,aa poker辅助包-确实是真的有辅助脚本(哔哩哔...
外挂机巧!拱趴大菠萝自动计算机... 外挂机巧!拱趴大菠萝自动计算机器人,aa poker辅助包-总是一直总是有辅助教程(哔哩哔哩)1、任...
外挂讲义!佛手在线是不是有挂,... 外挂讲义!佛手在线是不是有挂,竞技联盟透视插件-一贯真的是有辅助攻略(哔哩哔哩)1、玩家可以在佛手在...