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

相关内容

热门资讯

科技通报!wpk是真的还是假的... 科技通报!wpk是真的还是假的(辅助挂)确实有挂(讲解有挂)该软件可以轻松地帮助玩家将wpk是真的还...
一分钟了解!we-poker是... 一分钟了解!we-poker是什么软件(辅助挂)竟然真的有挂(有挂教程)1、we-poker是什么软...
重大通报!wepoker辅助器... 重大通报!wepoker辅助器软件下载(辅助挂)好像有挂(有挂方针)1、上手简单,内置详细流程视频教...
科普常识!德州透视脚本(辅助挂... 科普常识!德州透视脚本(辅助挂)都是真的有挂(有挂存在)1、德州透视脚本辅助器安装包、德州透视脚本辅...
解密关于!安装不了wepoke... 解密关于!安装不了wepoker(辅助挂)一直是有挂(确实有挂)1、完成安装不了wepoker有辅助...
推荐一款!如何下载wepoke... 推荐一款!如何下载wepoker安装包(辅助挂)一直真的有挂(有挂神器)1、操作简单,无需如何下载w...
实操分享!智星菠萝辅助(辅助挂... 实操分享!智星菠萝辅助(辅助挂)都是是有挂(有挂透明挂)1、许多玩家不知道智星菠萝辅助辅助怎么退出观...
玩家攻略!wepoker软件安... 玩家攻略!wepoker软件安装包(辅助挂)果然是有挂(有挂解惑)1)wepoker软件安装包免费钻...
大神推荐!德普之星怎么开辅助(... 大神推荐!德普之星怎么开辅助(辅助挂)一直是真的有挂(存在有挂)大神推荐!德普之星怎么开辅助(辅助挂...
科普攻略!来玩app破解版(辅... 科普攻略!来玩app破解版(辅助挂)本来存在有挂(竟然有挂)1、来玩app破解版有没有辅助教程、来玩...