storybook for angular

扶醉桌前 提交于 2020-03-01 11:50:37

storybook for angular 初试

文档

官方文档
angular示例

安装

依赖 @storybook/cli

zengwe$ npm i -g @storybook/cli

在angular项目下运行

zengwe$ getstorybook 

初始化成功后会在项目下增加连个文件夹
./.storybook
./src/stories
在stories下会有storybook的demo示例

import { action } from '@storybook/addon-actions';
import { linkTo } from '@storybook/addon-links';
import { ShowComponent } from '../app/viewer/show/show.component';
import { Button } from '@storybook/angular/demo';
import { ViewerModule } from '../app/viewer/viewer.module';
export default {
  title: 'Button',
  component: Button,
};

export const Text = () => ({
  component: Button,
  props: {
    text: 'Hello Button',
  },
});

export const Emoji = () => ({
  component: Button,
  props: {
    text: '😀 😎 👍 💯',
  },
});

Emoji.story = {
  parameters: { notes: 'My notes on a button with emojis' },
};

export const WithSomeEmojiAndAction = () => ({
  component: Button,
  props: {
    text: '😀 😎 👍 💯',
    onClick: action('This was clicked OMG'),
  },
});

WithSomeEmojiAndAction.story = {
  name: 'with some emoji and action',
  parameters: { notes: 'My notes on a button with emojis' },
};

export const ButtonWithLinkToAnotherStory = () => ({
  component: Button,
  props: {
    text: 'Go to Welcome Story',
    onClick: linkTo('Welcome'),
  },
});

ButtonWithLinkToAnotherStory.story = {
  name: 'button with link to another story',
};
export const ShowComponentStory = () => ({
  component: ShowComponent,
  name:'ShowComponent',
  moduleMetadata: {
    imports: [ViewerModule]
  },
  props: {
    txt: 'show text',
    evt: action('evt resturn')
  }
})
ShowComponentStory.story   = {
  name: '123',
  parameters: {
    notes: '123'
  }
}

运行

初始化完成时会在package.json中添加对应的脚本

{
    "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "storybook": "start-storybook -p 6006",
    "build-storybook": "build-storybook"
  }
}

storybook 的另一种写法

    text,
    number,
    boolean,
    array,
    select,
    radios,
    color,
    date,
    button,
  } from '@storybook/addon-knobs';
  const name = text('name', 'John Doe');
storiesOf('Addon|Actions', module)
.addParameters({
    knobs: {
      disableDebounce: true,
    },
  })
  .addDecorator(withKnobs)
.add('angular', () => ({
    component: ShowComponent,
    name:'ShowComponent',
    moduleMetadata: {
      imports: [ViewerModule]
    },
    props: {
      txt: name,
      evt: action('evt resturn')
    }
}));
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!