Storybook AngularJS (1.x) 插件
注意 此插件旨在与
@storybook/html
结合使用,自 Storybook 4 起可用。
安装
使用你喜欢的 📦 包管理器将插件安装到你的项目 devDependencies
中
npm
npm install --save-dev storybook-addon-angularjs
Yarn
yarn add --dev storybook-addon-angularjs
用法
此插件足够灵活,可让你选择如何编写 stories。
你的 stories 可以像这样简单
export default {
title: "QuoteCard",
decorators: [withAngularJs("myApp")],
};
export const simpleTemplate = () => `
<quote-card author="'Me'">
It works with a simple template!
</quote-card>
`;
但你也可以选择更高级的方式
import { withKnobs, text } from "@storybook/addon-knobs";
import { action } from "@storybook/addon-actions";
import { html, withAngularJs } from "storybook-addon-angularjs";
class MockedAppService {
constructor() {
this.message = "Mocked message";
}
}
function mockLoggingService($log) {
return {
log: function(message) {
$log.log(message);
},
}
}
export default {
title: "QuoteCard",
decorators: [withKnobs, withAngularJs /* OR */ withAngularJs("myApp")],
parameters: {
ng: {
module: "myApp", // optional when provided in the decorator
rebuild: undefined, // optional, indicates when to rebuild the story. Can be "always", "mount" (when switching stories) or "update" (when updating knobs or controls)
hooks: {
beforeCompile() {
// called once before compiling the the component
},
beforeUpdate(SomeService) {
// called before updating the component with new props
SomeService.setValue("Hi!");
},
},
mock: {
// When the app depends on modules which cannot be provided in the story you can mock them
modules: ["some.external.module"],
// You can mock / override constants here
constants: {
API_URL: "https://example.com",
},
// You can mock / override services here (dependency injection also works)
services: {
AppService: MockedAppService,
},
// You can mock / override factories here (dependency injection also works)
factories: {
LoggingService: mockLoggingService,
},
}
},
},
};
export const fancyTemplate = () => {
const content = text("Content", "It works with a fancy tagged template string!");
const author = text("Author", "Me");
const onEvt = action("clicked");
return html`
<quote-card author="${author}" on-click="${onEvt}(foo)">
{{${content}}}
</quote-card>
`;
};
你甚至可以使用 Markdown 文件编写 stories,并支持 MDX Story 格式。这对于为你的组件编写文档非常有用。
import { Meta, Story, Preview } from "@storybook/addon-docs/blocks";
import { withAngularJs } from "storybook-addon-angularjs";
<Meta title="Demos|MDX Demos" decorators={[withAngularJs("myApp")]} />
# Storybook Addon for AngularJS
This is a simple Quote Card:
<Preview>
<Story name="Simple Template" height="120px">
{`
<quote-card author="'Me'">
It works with a simple template!
</quote-card>
`}
</Story>
</Preview>
请在 示例子文件夹 中查看这些及更多示例。
API
withAngularJs(module?: string | string[])
Storybook decorator。可选择用于初始化 story 中使用的模块。
开发
准备开发环境
npx lerna bootstrap
构建示例 Storybook
npx lerna bootstrap
许可证
本源代码的使用受 MIT 风格许可证的约束,该许可证可在 LICENSE 文件中找到。