在 Vue3 中如何使用 styled-components

 更新时间:2024年05月11日 10:35:15   作者:来一杯奶茶呀!  
styled-components 的官方 Vue 版本目前已多年没有更新,而且只支持到 Vue2,那么,在 Vue3 中怎么才能使用到 styled-components 呢,下面给大家介绍在 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的文章就介绍到这了,更多相关Vue3使用 styled-components内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Vue自定义指令详解

    Vue自定义指令详解

    这篇文章主要为大家详细介绍了Vue自定义指令的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • VUE中$refs的基本用法举例

    VUE中$refs的基本用法举例

    ref 加在子组件上,用this.$refs.(ref值) 获取到的是组件实例,可以使用组件的所有方法, 在使用方法的时候直接this.$refs.(ref值).方法() 就可以使用了,这篇文章主要介绍了VUE中$refs的基本用法,需要的朋友可以参考下
    2022-12-12
  • 基于vue.js轮播组件vue-awesome-swiper实现轮播图

    基于vue.js轮播组件vue-awesome-swiper实现轮播图

    一般做移动端轮播图的时候,最常用的就是Swiper插件了,而vue.js也有一个轮播组件vue-awesome-swiper,用法跟swiper相似。接下来通过本文给大家详解讲解vue.js轮播组件vue-awesome-swiper实现轮播图实例代码,需要的朋友参考下
    2017-03-03
  • Vue加载组件、动态加载组件的几种方式

    Vue加载组件、动态加载组件的几种方式

    组件是Vue.js最强大的功能之一。组件可以扩展HTML元素,封装可重用的代码。这篇文章通过实例代码给大家介绍了Vue加载组件、动态加载组件的几种方式,需要的朋友参考下吧
    2018-08-08
  • vue-video-player视频播放器使用配置详解

    vue-video-player视频播放器使用配置详解

    这篇文章主要为大家详细介绍了vue-video-player视频播放器的使用和配置,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-10-10
  • vuejs如何解决浏览器切换页面后setInterval计时器停止执行的问题

    vuejs如何解决浏览器切换页面后setInterval计时器停止执行的问题

    setinterval()是定时调用的函数,可按照指定的周期(以毫秒计)来调用函数或计算表达式,这篇文章主要给大家介绍了关于vuejs如何解决浏览器切换页面后setInterval计时器停止执行的问题,需要的朋友可以参考下
    2024-01-01
  • vue实现二级导航栏效果

    vue实现二级导航栏效果

    这篇文章主要为大家详细介绍了vue实现二级导航栏效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-10-10
  • vue-router:嵌套路由的使用方法

    vue-router:嵌套路由的使用方法

    本篇文章主要介绍了vue-router:嵌套路由的使用方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02
  • Vue3实现全局公共函数的完整方案(创建、挂载、引用、使用)

    Vue3实现全局公共函数的完整方案(创建、挂载、引用、使用)

    本文详细介绍了如何在Vue3项目中创建、挂载、引用和使用全局公共函数,包括标准目录结构、创建公共函数、全局注册、页面使用以及规范,通过本文,新手开发者可以快速搭建企业级规范工具库,需要的朋友可以参考下
    2026-03-03
  • Vue和React中diff算法的区别及说明

    Vue和React中diff算法的区别及说明

    React和Vue都使用虚拟DOM和diff算法来更新DOM,但它们在实现上有所不同,React采用基于索引的比较,Vue采用双端比较算法,React在比较时不复用不同类型的节点,而Vue会优先复用两端相同的节点,React对key的依赖较高,而Vue在没有key时也能通过双端比较优化
    2025-03-03

最新评论