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

你

相关内容

热门资讯

玩家必备教程!透视挂!喜临门a... 玩家必备教程!透视挂!喜临门ai透视挂多功能插件,确实真的有透视挂助手秘籍AI 是一款可以让一直输的...
盘点十款透视!四川麻将家园ai... 盘点十款透视!四川麻将家园ai透视多功能插件,确实是真的有透视助手大纲是一款可以让一直输的玩家,快速...
盘点一款开挂!掌心娱乐ai开挂... 您好,掌心娱乐这款游戏可以开挂的,确实是有挂的,需要了解加微【485275054】很多玩家在这款游戏...
我来向大家传授透视挂!七色城a... 我来向大家传授透视挂!七色城ai透视挂多功能插件,最初有透视挂技巧诀窍AI 智能体黑科技再上新,自动...
大家学习交流开挂!飘飘游戏ai... 您好,飘飘游戏这款游戏可以开挂的,确实是有挂的,需要了解加微【485275054】很多玩家在这款游戏...
玩家必备教程辅助!边锋老友麻将... 玩家必备教程辅助!边锋老友麻将ai辅助多功能插件,确实是真的有辅助神器教材AI 是一款可以让一直输的...
安装教程透视挂!江陵城ai透视... 安装教程透视挂!江陵城ai透视挂多功能插件,切实真的是有透视挂软件策略是一款可以让一直输的玩家,快速...
玩家必看攻略透视挂!心悦辽宁麻... 玩家必看攻略透视挂!心悦辽宁麻将ai透视挂多功能插件,一直是真的有透视挂插件讲义是一款可以让一直输的...
推荐十款透视挂!大唐撸麻雀ai... 一款 AI 游戏攻略黑科技工具上线,查攻略效率直接开挂。它整合版本更新、装备搭配、BOSS 打法、牌...
推荐十款透视挂!蜀州麻将一大福... 您好,推荐十款透视挂!蜀州麻将一大福ai透视挂多功能插件,原先有透视挂神器技法这款游戏可以开挂的,确...