vue scoped的作用

本文介绍了在Vue.js中如何使用scoped属性限定样式作用范围及通过lang属性指定LESS作为样式语言的方法。通过一个具体示例展示了如何组织组件的样式代码,并保持良好的封装性和可维护性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在 vue的组件化开发中,我们会给  <style></style> 中加上一些参数,

比如:scoped  ,或者  lang="less";


scoped 的意思是下面的样式的作用域就是当前这个组件,比如aa.vue这个组件,

所有的样式只在当前这个组件中有效果,那么lang="less"的意思是,声明我是用的

是less 还是 css 还是sass等等;


下面是个小例子: 主要看style部分,这里我使用了less,并且设置样式只在当前

组件中有效果

<template>
<div class="us">
<h3 @click="info1">关于我们--2018 更新</h3>
<p v-if="skate">{{skate}}</P>
<h3 @click="info2">join us --2018更新</h3>
<P v-if="skate">{{music}}</P>
</div>
</template>


<script>
import axios from "axios";
import Mock from "mockjs";
Mock.mock('http://www.bai.com',{
'skate':'大家来自天南海北,因为轮滑开始有了交集,时光不老,我们不散!',
'music':`我们喜欢音乐,我们喜欢轮滑,我们喜欢飞的感觉,仿佛有了翅膀,这里无拘无束,
只是让你不再寂寞的一个人飞,欢迎加入我们,相关活动我们会在活动页面发布,
请注意浏览!`,
})

export default {
name:"Us",
data(){
return{
skate:"",
music:""
}
},
methods:{
info1(){
let that=this;
axios.get("http://www.bai.com")
.then(function(response){
// console.log(response.data.skate);
that.skate=response.data.skate;
})
.catch(function(response){
})
},
info2(){
let that=this;
axios.get("http://www.bai.com")
.then(function(response){
that.music=response.data.music;
})
.catch(function(response){
})
}
}
}
</script>



<style lang="less" scoped>
@base:32;
.us{
width:100%;
height:auto;
box-sizing:border-box;
h3{
box-sizing:border-box;
font-size:unit(8/@base,rem);
height:unit(30/@base,rem);
padding:unit(3/@base,rem);
padding-top:unit(5/@base,rem);
color:#0084ff;
border-bottom:1px solid #f5f5f5;
}
p{
padding:unit(10/@base,rem);
color:#333;
font-size:unit(10/@base,rem);
line-height:unit(20/@base,rem);
}
}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值