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

相关内容

热门资讯

透视曝光!hhpoker透视方... 透视曝光!hhpoker透视方法,hhpoker有没有作弊辅助-本来真的有辅助神器(哔哩哔哩)1、金...
透视详情!wpk是真的还是假的... 透视详情!wpk是真的还是假的,wpk真的有透视嘛-本来存在有辅助工具(哔哩哔哩)1、实时透视辅助更...
透视揭幕!购买的wpk辅助在哪... 透视揭幕!购买的wpk辅助在哪里下载,wpk软件是真的吗-真是一直总是有辅助插件(哔哩哔哩)是不是有...
透视分享!wepoker有没有... 透视分享!wepoker有没有挂,wepoker数据分析-一贯是真的有辅助脚本(哔哩哔哩)1、免费脚...
透视了解!wepoker私人局... 透视了解!wepoker私人局有透视吗,WePoKer辅助器-确实一直都是有辅助教程(哔哩哔哩)1、...
透视辅助!wpk真的有透视嘛,... 透视辅助!wpk真的有透视嘛,wpk真吗-本来是真的有辅助app(哔哩哔哩)1、透视辅助!wpk真的...
透视详情!wepoker私人局... 透视详情!wepoker私人局辅助器怎么用,wepokerh5破解-确实一直都是有辅助工具(哔哩哔哩...
透视详情!智星德州插件2024... 透视详情!智星德州插件2024最新版,pokemmo脚本-一直真的有辅助脚本(哔哩哔哩)1、上手简单...
透视分享!hhpkoer辅助挂... 透视分享!hhpkoer辅助挂是真的吗,hhpoker德州牛仔视频-果然有辅助神器(哔哩哔哩)1、完...
透视教你!德州局怎么透视,模拟... 透视教你!德州局怎么透视,模拟器打开hhpoker-切实一直总是有辅助技巧(哔哩哔哩)1、点击下载安...