在 Vue3 中使用 styled-components

2024-05-07 19:36

本文主要是介绍在 Vue3 中使用 styled-components,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

随着组件化时代的兴起,前端应用开始采用组件级别的 CSS 封装:通过 JavaScript 声明和抽象样式,以提高组件的可维护性。在组件加载时动态加载样式,并动态生成类名,从而避免全局污染。 styled-components 是其中的杰出代表。 正如其名称所示,styled-components 以组件的形式声明样式,将样式与组件分离,实现逻辑组件与展示组件的分离。

styled-components 的官方 Vue 版本目前已多年没有更新,而且只支持到 Vue2。那么,在 Vue3 中怎么才能使用到 styled-components 呢?在 Github 翻了一下,大部分复刻 vue-styled-components 的库要么不再更新,要么没有任何文档和说明。

不过还是找到一个质量还可以的库:

vue-styled-components

查看了一下文档,还原了大部分核心 API,使用上与官方 styled-components 差别不大。下面来看看如何使用。

使用

安装

npm i @vvibe/vue-styled-components

styled原生组件

<script setup lang="ts">
import { styled } from '@vvibe/vue-styled-components'const StyledLink = styled('a')`color: darkred;
`
</script><template><StyledLink>链接</StyledLink>
</template>

也可以直接链式调用

<script setup lang="ts">
import { styled } from '@vvibe/vue-styled-components'const StyledLink = styled.a`color: darkred;
`
</script><template><StyledLink>链接</StyledLink>
</template>

响应式样式

<script setup lang="ts">
import { ref } from 'vue'
import { styled } from '@vvibe/vue-styled-components'const borderColor = ref('darkred')
const inputProps = { borderColor: String }
const StyledInput = styled('input', inputProps)`width: 100%;height: 40px;padding: 4px 8px;border: 1px solid ${(props) => props.borderColor};border-radius: 8px;
`const input = () => (borderColor.value = 'forestgreen')
const focus = () => (borderColor.value = 'skyblue ')
const blur = () => (borderColor.value = 'darkred')
</script><template><StyledInput placeholder="Type something" :borderColor="borderColor" @input="input" @focus="focus" @blur="blur" />
</template>

扩展样式

<script setup lang="ts">
import { styled } from '@vvibe/vue-styled-components';const BlueButton = styled.button`width: 120px;height: 40px;margin-right: 8px;padding: 4px 8px;border-radius: 9999px;box-shadow: 0 0 4px rgba(0, 0, 0, 0.3);background-color: skyblue;font-weight: bold;
`;
const RedButton = styled(BlueButton)`background-color: darkred;color: white;
`;
</script><template><BlueButton>Blue Button</BlueButton><RedButton>Red Button</RedButton>
</template>

动画

<script setup lang="ts">
import { styled, keyframes } from '@vvibe/vue-styled-components'const rotate = keyframes`from {transform: rotate(0deg);}to {transform: rotate(360deg);}
`
const translate = keyframes`0 {transform: translateX(0);}50% {transform: translateX(250%);}60% {transform: rotate(360deg);}
`const StyledBaseDiv = styled.div`display: inline-block;width: 100px;height: 100px;
`const StyledRotateDiv = styled(StyledBaseDiv)`background-color: skyblue;animation: ${rotate} 2s linear infinite;
`const StyledTranslateDiv = styled(StyledBaseDiv)`margin-left: 10px;background-color: darkred;animation: ${translate} 2s ease infinite alternate;
`
</script><template><StyledRotateDiv /><StyledTranslateDiv />
</template>

主题

<script setup lang="ts">
import { styled, ThemeProvider } from '@vvibe/vue-styled-components'const Wrapper = styled.div`display: flex;justify-content: space-around;
`const StyledLink = styled.a`margin-right: 8px;color: ${(props) => props.theme.primary};font-weight: bold;
`
</script><template><Wrapper><a>This a normal link</a><ThemeProvider :theme="{ primary: 'red' }"><StyledLink>This a theming link</StyledLink></ThemeProvider></Wrapper>
</template>

更多细节查看文档:https://vue-styled-components.com

这篇关于在 Vue3 中使用 styled-components的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

[SCF+wetest+jmeter]云性能压测工具使用方案

前言 压测太难?局域网压力无法判断服务器网络指标?无法产生非常大的并发量?云性能太贵? 也许我们可以把各种简单的工具拼起来进行压力测试! 准备 https://cloud.tencent.com/product/scf https://cloud.tencent.com/product/lm 云压测背景 常规压测工具,所有请求均从本地出发,走局域网可能导致网络请求的实际效果不够真实

IDEA中使用maven配置mybatis-------mybatis(一)

IDEA下通过maven配置mybatis 创建maven项目,路径为:file–setting-maven–maven-archetye-webapp; 创建mybatis目录,按箭头所示依次点击,创建一个mybatis的文件编写目录 修改pom.xml配置文件,刚开始可能红框中的字体为红色,等待maven自动下载完成之后即可 mysql客户端配置 添加log4j <

vue3显示element-plus所有icon

效果 代码 <template><div style="display: flex;flex-wrap: wrap"><component :is="name" style="width: 2rem; margin-left: 2rem" v-for="(name,index) in icons" :index="index" :key="index"></component></div>

bimface开发实战-vue版

效果 演示地址 框架 bimface + vue3.0 代码地址 gitee地址 使用 yarn install yarn serve

vue3.0 v-model 的使用

前言 组件功能:把 el-switch 的值 false/true, 动态绑定输出为 0, 1 组件代码 封装el-switch组件,当el-switch的值为false,输出值为0;当el-switch的值为true,输出值为1; <template><el-switch v-model="switchValue" @change="changeEvent"></el-switch

sublime text ctags使用心得

ctags文件需要另行下载,然后添加到Windows的Path路径下面。 ctags -R -f .tags生成  .tags文件,然后在sublime下就可以用ctrl+t ctrl+t来跳转,用ctrl+t ctrl+b来返回到原来位置 使用ctrl+t、ctrl+r 两个连续组合键,创建.tags工程符号索引文件(此时Sublime Text 2会自动调用第一步安装的ctag

vimperator的使用

Vimium 其实是一款 Chrome 浏览器中的插件,而 Vimperator 则是 FireFox 火狐浏览器的插件     j:向下细微滚动窗口   k:向上细微滚动窗口          ctrl+d:向下滚动半个屏幕   ctrl+u:向上移动半个屏幕     g+g(连续按两下g):回到顶部     G:到达页面底部     H:后退   L: 前进

使用frp把内网http服务映射到外部网络

使用的软件为frp,地址为: https://github.com/fatedier/frp A fast reverse proxy to help you expose a local server behind a NAT or firewall to the internet. 一个快速的反向代理,可以帮助你暴露在NAT或防火墙后面的内网本地的服务器给互联网。 Windows7  本

1、swift学习-字典的基本使用

1、创建一个字典          var dic:Dictionary<String,String> = ["三国演艺":"罗贯中","水浒传":"施耐庵","红楼梦":"曹雪芹","西游记":"吴承恩"]; 2、打印字典的值     println(dic);     结果如下图:      3、访问字典中某一个键值     var

Swift5 导航栏的使用 2022年11月更新

Swift 导航栏的使用 一、基本使用 1.1 创建导航栏 在AppDelegate 如下方法中添加创建导航栏的代码: func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> B