vue.jsvue.js·

Vue 如何在 style 标签里使用变量

投稿時間:2024-08-28 21:30:05閲覧数:41
専門記事
転載は出所を明記してください

在Vue项目开发中,有时候我们需要在style标签中使用我们所定义的JS变量来编写CSS。

根据官方文档,我们可以使用v-bind来实现这个目的

选项式API:

<template>
  <div class="text">hello</div>
</template>
 
<script>
export default {
  data() {
    return {
      color: 'red'
    }
  }
}
</script>
 
<style>
.text {
  color: v-bind(color);
}
</style>

组合式API:

<script setup>
import { ref } from 'vue'
const theme = ref({
    color: 'red',
})
</script>

<template>
  <p>hello</p>
</template>

<style scoped>
p {
  color: v-bind('theme.color');
}
</style>

还有一个问题,如果我们的变量是数字,但是我们想要设置像素px怎么办?

以下以组合式API为例(选项式API方法大同小异),提供两种解决方案。

一种是使用 computed 计算属性改变它

<script setup>
import { computed } from 'vue';
const props = defineProps({ size: Number });
 
const sizePx = computed(() => `${props.size}px`)
</script>
 
<template>
  <p>hello</p>
</template>
 
<style scoped>
p {
  font-size: v-bind(sizePx);
}
</style>

还有一种方式是使用 CSS 计算属性calc

<script setup>
defineProps({ size: Number });
</script>

<template>
  <p>hello</p>
</template>

<style scoped>
p {
  font-size: calc(1px * v-bind(size));
}
</style>

那么如何在代码中使用style属性呢?

也很简单,根据官方文档,在style标签中加上module,然后就能通过$style来使用style中的CSS属性。

<template>
  <p :class="$style.red">This should be red</p>
</template>
 
<style module>
.red {
  color: red;
}
</style>

还可以给style的module attribute设置不同的名字,从而调用不同style标签中的属性

<template>
  <p :class="classes1.red">This should be red</p>
</template>
 
<style module="classes1">
.red {
  color: red;
}
</style>
 
<style module="classes2">
.red {
  color: green;
}
</style>

如果想要在<script setup>中使用,可以使用 useCssModule

import { useCssModule } from 'vue';

const classes1 = useCssModule('classes1');

const classes2 = useCssModule('classes2');


コメント欄

まだコメントがありません。最初のコメントを投稿しましょう!

弦圈热门内容

Djano云服务器部署 uwsgi+nginx+https部署

Django启动服务器的命令runserver,其实也是启动了一个自带的uwsgi服务。如果想要一直在后台不停的运行django项目,需要部署uwsgi服务器。uwsgi安装官方文档:安装uWSGI — uWSGI 2.0 文档 (uwsgi-docs-zh.readthedocs.io)可以直接用pip安装uwsgi:pip install uwsgi在django项目根地址,即跟manage.py同目录下新建配置文件uwsgi.ini,如:mysite1/uwsgi.iniuwsgi.ini必须以[uwsgi]开头,内容如下:[uwsgi] chdir=/mysite1 module=mysite1.wsgi:application socket=127.0.0.1:8000 master=True pidfile=uwsgi.pid vacuum=True max-requests=5000 daemonize=uwsgi.logchdir为django项目根地址,socket为django启动服务器的ip地址,daemonize为日志地址。注意,每个django项目中自动生成一 ...

token简介以及python计算token的方法

后端登陆实现需要用到token机制或者cookies机制。Token和cookies都可以用来存放用户信息。但是token可以将信息存放在浏览器中的localstorage中,不占用服务器内存,而cookies则需要在每次请求中送往服务器中,吃服务器资源。同时,token作为一种比cookies更新的技术,有更多的优势,可以参考token和cookie的区别。每个用户都可以对应一个token值。Token可以由用户名+密码+时间,经过哈希加密得到,也可以直接由用户名和密码经过加密算法加密后得到。复杂程度取决于自己。加密后得到的token值,存放在会话session中。Python使用哈希算法进行加密,计算token值:import hashlib md5 = hashlib.md5() md5.update((username+password+"1258"+str(time.time())).encode()) token = md5.hexdigest()也可以使用python的pip库pyjwt来实现加密,计算token值。$ pip install pyjwtJwt输入的是字 ...

为什么有了uwsgi 还要 nginx 服务器?

有关nginx的简单介绍,详见Nginx服务器反向代理。uwsgi是python的一个通信协议,同时也是一种web服务器,而nginx则是高性能反向代理的web服务器。在Django项目服务器部署中,uwsgi几乎是不可替代的。然而部署好了uwsgi,其实django接口已经能够响应请求,为什么还要额外配置nginx服务器?因为,相比于直接将真实地址暴露在公网上,在外面套一层nginx安全性更高,具体如下:安全问题,程序不能直接被浏览器访问到,而是通过nginx,nginx只开放某个接口,uwsgi本身是内网接口,这样运维人员在nginx上加上安全性的限制,可以达到保护程序的作用。负载均衡问题,一个uwsgi很可能不够用,即使开了多个work也是不行,毕竟一台机器的cpu和内存都是有限的,有了nginx做代理,一个nginx可以代理多台uwsgi完成uwsgi的负载均衡。静态文件处理效率问题,用django或是uwsgi这种东西来负责静态文件的处理是很浪费的行为,而且他们本身对文件的处理也不如nginx好,所以整个静态文件的处理都直接由nginx完成,静态文件的访问完全不去经过uwsg ...