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

相关文章

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

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

vue基于ElementUI动态设置表格高度的3种方法

《vue基于ElementUI动态设置表格高度的3种方法》ElementUI+vue动态设置表格高度的几种方法,抛砖引玉,还有其它方法动态设置表格高度,大家可以开动脑筋... 方法一、css + js的形式这个方法需要在表格外层设置一个div,原理是将表格的高度设置成外层div的高度,所以外层的div需要

Vue项目中Element UI组件未注册的问题原因及解决方法

《Vue项目中ElementUI组件未注册的问题原因及解决方法》在Vue项目中使用ElementUI组件库时,开发者可能会遇到一些常见问题,例如组件未正确注册导致的警告或错误,本文将详细探讨这些问题... 目录引言一、问题背景1.1 错误信息分析1.2 问题原因二、解决方法2.1 全局引入 Element

详解Vue如何使用xlsx库导出Excel文件

《详解Vue如何使用xlsx库导出Excel文件》第三方库xlsx提供了强大的功能来处理Excel文件,它可以简化导出Excel文件这个过程,本文将为大家详细介绍一下它的具体使用,需要的小伙伴可以了解... 目录1. 安装依赖2. 创建vue组件3. 解释代码在Vue.js项目中导出Excel文件,使用第三

vue解决子组件样式覆盖问题scoped deep

《vue解决子组件样式覆盖问题scopeddeep》文章主要介绍了在Vue项目中处理全局样式和局部样式的方法,包括使用scoped属性和深度选择器(/deep/)来覆盖子组件的样式,作者建议所有组件... 目录前言scoped分析deep分析使用总结所有组件必须加scoped父组件覆盖子组件使用deep前言

VUE动态绑定class类的三种常用方式及适用场景详解

《VUE动态绑定class类的三种常用方式及适用场景详解》文章介绍了在实际开发中动态绑定class的三种常见情况及其解决方案,包括根据不同的返回值渲染不同的class样式、给模块添加基础样式以及根据设... 目录前言1.动态选择class样式(对象添加:情景一)2.动态添加一个class样式(字符串添加:情

使用Vue.js报错:ReferenceError: “Vue is not defined“ 的原因与解决方案

《使用Vue.js报错:ReferenceError:“Vueisnotdefined“的原因与解决方案》在前端开发中,ReferenceError:Vueisnotdefined是一个常见... 目录一、错误描述二、错误成因分析三、解决方案1. 检查 vue.js 的引入方式2. 验证 npm 安装3.

vue如何监听对象或者数组某个属性的变化详解

《vue如何监听对象或者数组某个属性的变化详解》这篇文章主要给大家介绍了关于vue如何监听对象或者数组某个属性的变化,在Vue.js中可以通过watch监听属性变化并动态修改其他属性的值,watch通... 目录前言用watch监听深度监听使用计算属性watch和计算属性的区别在vue 3中使用watchE

Vue3 的 shallowRef 和 shallowReactive:优化性能

大家对 Vue3 的 ref 和 reactive 都很熟悉,那么对 shallowRef 和 shallowReactive 是否了解呢? 在编程和数据结构中,“shallow”(浅层)通常指对数据结构的最外层进行操作,而不递归地处理其内部或嵌套的数据。这种处理方式关注的是数据结构的第一层属性或元素,而忽略更深层次的嵌套内容。 1. 浅层与深层的对比 1.1 浅层(Shallow) 定义

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template