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

你

相关内容

热门资讯

2027版外挂!桃源麻将外挂多... 2027版外挂!桃源麻将外挂多功能AI插件,桃源麻将本来有外挂插件1、在桃源麻将插件功能辅助器技巧中...
2027版开挂!圈友建瓯麻将开... 2027版开挂!圈友建瓯麻将开挂多功能AI技巧,圈友建瓯麻将确实真的是有开挂技巧1、游戏颠覆性的策略...
2027版辅助!欢乐互娱辅助多... 2027版辅助!欢乐互娱辅助多功能AI技巧,欢乐互娱一直真的有辅助技巧1、欢乐互娱免费辅助多个强度级...
2027版辅助!闲来陕西麻将辅... 2027版辅助!闲来陕西麻将辅助多功能AI技巧,闲来陕西麻将一直有辅助技巧1、闲来陕西麻将脚本辅助下...
2027版辅助器!华淳麻将辅助... 2027版辅助器!华淳麻将辅助器多功能AI软件,华淳麻将好像真的是有辅助器软件1、不需要AI权限,帮...
2027版辅助器!一乐棋牌辅助... 2027版辅助器!一乐棋牌辅助器多功能AI攻略,一乐棋牌都是是真的有辅助器攻略小薇(辅助器软件下载)...
2027版开挂!友聚麻将开挂多... 2027版开挂!友聚麻将开挂多功能AI攻略,友聚麻将切实有开挂攻略友聚麻将破解侠是真的助透视。每个模...
2027版透视挂!369互娱麻... 2027版透视挂!369互娱麻将透视挂多功能AI插件,369互娱麻将都是真的有透视挂插件1、在369...
2027版辅助器!博乐填大坑辅... 2027版辅助器!博乐填大坑辅助器多功能AI教程,博乐填大坑切实真的是有辅助器教程博乐填大坑能透视中...
2027版外挂!腾迅广东麻将外... 2027版外挂!腾迅广东麻将外挂多功能AIapp,腾迅广东麻将真是真的是有外挂app1、打开软件启动...