在 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实现Google第三方登录的示例代码

    Vue实现Google第三方登录的示例代码

    本文记录作者在vue项目中使用到Google第三方登录,查询到的资料文档也不详细,故此把自己所遇到的坑及问题详细的记录下来。
    2021-07-07
  • Vue如何实现自动触发功能

    Vue如何实现自动触发功能

    这篇文章主要介绍了Vue如何实现自动触发功能,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • vue3.0公共组件自动导入的方法实例

    vue3.0公共组件自动导入的方法实例

    这篇文章主要给大家介绍了关于vue3.0公共组件自动导入的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • elementUI select组件value值注意事项详解

    elementUI select组件value值注意事项详解

    这篇文章主要介绍了elementUI select组件value值注意事项详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-05-05
  • 使用Bootstrap4 + Vue2实现分页查询的示例代码

    使用Bootstrap4 + Vue2实现分页查询的示例代码

    本篇文章主要介绍了使用Bootstrap4 + Vue2实现分页查询的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-12-12
  • Vue.Draggable拖拽功能的配置使用方法

    Vue.Draggable拖拽功能的配置使用方法

    这篇文章主要为大家详细介绍了Vue.Draggable拖拽功能配置使用的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-12-12
  • Vue集成three.js并加载glb、gltf、FBX、json模型的场景分析

    Vue集成three.js并加载glb、gltf、FBX、json模型的场景分析

    这篇文章主要介绍了Vue集成three.js,并加载glb、gltf、FBX、json模型,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-09-09
  • 老生常谈Vue中的侦听器watch

    老生常谈Vue中的侦听器watch

    开发中我们在data返回的对象中定义了数据,这个数据通过插值语法等方式绑定到template中,这篇文章主要介绍了Vue中的侦听器watch,需要的朋友可以参考下
    2022-10-10
  • Vue3基于rem比例H5缩放方案示例详解

    Vue3基于rem比例H5缩放方案示例详解

    这篇文章主要为大家介绍了Vue3基于rem比例H5缩放方案示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-11-11
  • Electron自动更新失效报错Error: Object has been destroyed的问题解决

    Electron自动更新失效报错Error: Object has been destroyed的问题解决

    本文主要讲解如何解决 Error: Object has been destroyed 这个 Electron 中最常见的问题,以及 Electron 自动更新的流程,文中通过代码示例给大家讲解的非常详细,需要的朋友可以参考下
    2024-01-01

最新评论