storybook vue@3.0

2023-10-10 00:40
文章标签 vue 3.0 storybook

本文主要是介绍storybook vue@3.0,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Storybook是一个开源工具,用于独立开发React、Vue和Angular的UI组件。它能有组织和高效地构建UI组件。

这里介绍vue@3.0

1、初始化项目

npx sb@next init 

2、直接运行

npm run storybook

运行结果
截屏2021-04-15 下午1.40.19.png

3、配置scss
需要安装sass-loader和node-sass,这里是自己写的FFButton,icon用的是elementUI的 所以样式需要配置scss

npm install sass-loader@8.0.2 node-sass@4.12.0

在.storybook/main.js 下需要配置

const path = require('path');webpackFinal: async (config, { configType }) => {config.module.rules.push({test: /\.scss$/,use: ['style-loader', 'css-loader', 'sass-loader'],include: path.resolve(__dirname, '../'),});return config;}

4、手写ff-button组件,放在src/components/ff-button目录下
variables.scss 是配置全局颜色的文件,这个组件的样式没有单独拎出来,写文档图方便,就放在组件里面了

<template><div class="button"><button:class="['ff-button', type ? 'ff-button--' + type : '',{'is-disabled': buttonDisabled,}]":disabled="buttonDisabled"@click="handleClick"><i v-if="loading" class="el-icon-loading"></I><i v-if="icon && !loading" :class="icon"></I><span v-if="$slots.default"><slot></slot></span></button></div></template><script>
import { computed, defineComponent } from 'vue'
export default defineComponent({name: 'FFButton',props: {type: {type: String ,default: 'default',validator: (val) => {return ['default','primary','success','warning','info','danger','text',].includes(val)},},disabled: Boolean,icon: {type: String,default: '',},loading: Boolean,},emits: ['click'],setup(props, ctx) {const buttonDisabled = computed(() => {return props.disabled})//methodsconst handleClick = (evt) => {ctx.emit('click', evt)}return{buttonDisabled,handleClick,}}
})
</script><style lang="scss" scoped>
@import "../../styles/variables.scss";.button{width: 80px;height: 40px;margin:0px 5px;
}.ff-button{width: 100%;height: 100%;border-radius: 5px;font-size: 14px;color: #FFFFFF;border: none;cursor: pointer;
}
.ff-button:focus {outline: none;
}
.ff-button:hover{opacity: 0.5;
}.ff-button:active::before {opacity: 0.1;
}
.ff-button--default{background-color: $default;color: #333333;border: 1px #e1e1e1 solid;
}
.ff-button--primary{background-color: $primary;
}
.ff-button--success{background-color: $success;
}
.ff-button--warning{background-color: $warning;
}
.ff-button--info{border: none;background-color: $info;
}
.ff-button--danger{background-color: $danger;
}
.ff-button--text{background-color: $text;
}
.is-disabled{background-color: #f5f7fa;border-color: #e4e7ed;color: #c0c4cc;cursor: not-allowed;
}
</style>

icon 图标配置,是借用elementUI的字体图标
截屏2021-04-15 下午1.54.56.png

在src/main.js中引入
import ‘…/src/assets/css/icon.css’
这样icon图标就可以使用了

5、写storybook生成文档
在stories下面新建ffButton.stories.js,需要引入组件,引入icon.scss

import FFButton from '../components/ff-button/ff-button.vue';
import '../../src/assets/css/icon.css'

配置组件的事件或动态数据

import { action } from '@storybook/addon-actions';export const actionsData = {onHandlClick: action('click'),
};const Template = (args) => ({// Components used in your story `template` are defined in the `components` objectcomponents: { FFButton },// The story's `args` need to be mapped into the template through the `setup()` methodsetup() {return { args };},methods: actionsData,// 新增配置template: '<FFButton v-bind="args" type="default"  :icon="`el-icon-edit`" :disabled="false" @click="onHandlClick($event)">default</FFButton>',
});

由于事件的点击回调的方法是动态配置的所以还要需要加上excludeStories: /.*Data$/,不然会报错

export default {title: 'Example/FFButton',component: FFButton,excludeStories: /.*Data$/,
};

说明:
title是左侧目录结构
component是组件本身
argTypes提供有关未明确设置的args的信息,还可以用来args信息备注
template.bind({})是一个标准javascript复制功能技术

ffButton.stories.js完整代码

import FFButton from '../components/ff-button/ff-button.vue';
import '../../src/assets/css/icon.css'
import { action } from '@storybook/addon-actions';export default {title: 'Example/FFButton',component: FFButton,argTypes: {},excludeStories: /.*Data$/,};
export const actionsData = {onHandlClick: action('click'),
};const Template = (args) => ({// Components used in your story `template` are defined in the `components` objectcomponents: { FFButton },// The story's `args` need to be mapped into the template through the `setup()` methodsetup() {return { args };},methods: actionsData,// And then the `args` are bound to your component with `v-bind="args"`template: '<FFButton v-bind="args" type="default"  :icon="`el-icon-edit`" :disabled="false" @click="onHandlClick($event)">按钮</FFButton>',
});export const Default = Template.bind({});
Default.args = {icon:'el-icon-edit',type:'default'
};export const Primary = Template.bind({template: '<FFButton type="Primary" :icon="`el-icon-share`" @click="onHandlClick($event)">按钮</FFButton>'});
Primary.args = {icon:'el-icon-share',type:'primary'
};export const success = Template.bind({template: '<FFButton type="success" :icon="`el-icon-delete`" @click="onHandlClick($event)">按钮</FFButton>'});
success.args = {icon:'el-icon-delete',type:'success'
};export const warning_loading = Template.bind({template: '<FFButton type="warning" :loading="true" @click="onHandlClick($event)">按钮</FFButton>'});
warning_loading.args = {loading:true,type:'warning'
};export const info = Template.bind({template: '<FFButton type="info" @click="onHandlClick($event)">按钮</FFButton>'});
info.args = {type:'info'
};export const danger = Template.bind({template: '<FFButton type="danger" @click="onHandlClick($event)">按钮</FFButton>'});
danger.args = {type:'danger'
};

效果如下

截屏2021-04-15 下午2.50.17.png

截屏2021-04-15 下午2.52.25.png

6、安装插件

Storybook有 数百个可重复使用的插件打包为NPM模块。

npm下载包,在.storybook/main.js下addons配置就可以。

"addons": ["@storybook/addon-links","@storybook/addon-essentials","@storybook/addon-actions","@storybook/addon-storysource"],

具体配置查看storybook官网/Install addons

这篇关于storybook vue@3.0的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/176719

相关文章

Vuex Actions多参数传递的解决方案

《VuexActions多参数传递的解决方案》在Vuex中,actions的设计默认只支持单个参数传递,这有时会限制我们的使用场景,下面我将详细介绍几种处理多参数传递的解决方案,从基础到高级,... 目录一、对象封装法(推荐)二、参数解构法三、柯里化函数法四、Payload 工厂函数五、TypeScript

Vue3使用router,params传参为空问题

《Vue3使用router,params传参为空问题》:本文主要介绍Vue3使用router,params传参为空问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录vue3使用China编程router,params传参为空1.使用query方式传参2.使用 Histo

vue使用docxtemplater导出word

《vue使用docxtemplater导出word》docxtemplater是一种邮件合并工具,以编程方式使用并处理条件、循环,并且可以扩展以插入任何内容,下面我们来看看如何使用docxtempl... 目录docxtemplatervue使用docxtemplater导出word安装常用语法 封装导出方

Vue中组件之间传值的六种方式(完整版)

《Vue中组件之间传值的六种方式(完整版)》组件是vue.js最强大的功能之一,而组件实例的作用域是相互独立的,这就意味着不同组件之间的数据无法相互引用,针对不同的使用场景,如何选择行之有效的通信方式... 目录前言方法一、props/$emit1.父组件向子组件传值2.子组件向父组件传值(通过事件形式)方

Vue 调用摄像头扫描条码功能实现代码

《Vue调用摄像头扫描条码功能实现代码》本文介绍了如何使用Vue.js和jsQR库来实现调用摄像头并扫描条码的功能,通过安装依赖、获取摄像头视频流、解析条码等步骤,实现了从开始扫描到停止扫描的完整流... 目录实现步骤:代码实现1. 安装依赖2. vue 页面代码功能说明注意事项以下是一个基于 Vue.js

Vue中动态权限到按钮的完整实现方案详解

《Vue中动态权限到按钮的完整实现方案详解》这篇文章主要为大家详细介绍了Vue如何在现有方案的基础上加入对路由的增、删、改、查权限控制,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、数据库设计扩展1.1 修改路由表(routes)1.2 修改角色与路由权限表(role_routes)二、后端接口设计

Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)

《Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)》文章介绍了如何使用dhtmlx-gantt组件来实现公司的甘特图需求,并提供了一个简单的Vue组件示例,文章还分享了一... 目录一、首先 npm 安装插件二、创建一个vue组件三、业务页面内 引用自定义组件:四、dhtmlx

Vue ElementUI中Upload组件批量上传的实现代码

《VueElementUI中Upload组件批量上传的实现代码》ElementUI中Upload组件批量上传通过获取upload组件的DOM、文件、上传地址和数据,封装uploadFiles方法,使... ElementUI中Upload组件如何批量上传首先就是upload组件 <el-upl

Vue3中的动态组件详解

《Vue3中的动态组件详解》本文介绍了Vue3中的动态组件,通过`component:is=动态组件名或组件对象/component`来实现根据条件动态渲染不同的组件,此外,还提到了使用`markRa... 目录vue3动态组件动态组件的基本使用第一种写法第二种写法性能优化解决方法总结Vue3动态组件动态

部署Vue项目到服务器后404错误的原因及解决方案

《部署Vue项目到服务器后404错误的原因及解决方案》文章介绍了Vue项目部署步骤以及404错误的解决方案,部署步骤包括构建项目、上传文件、配置Web服务器、重启Nginx和访问域名,404错误通常是... 目录一、vue项目部署步骤二、404错误原因及解决方案错误场景原因分析解决方案一、Vue项目部署步骤