当前位置: 首页> 汽车> 报价 > 商业网_站长之家官网查询_网站优化与seo_许昌seo推广

商业网_站长之家官网查询_网站优化与seo_许昌seo推广

时间:2025/7/16 23:07:30来源:https://blog.csdn.net/qq_45688289/article/details/146079485 浏览次数: 1次
商业网_站长之家官网查询_网站优化与seo_许昌seo推广

文章目录

  • 一、scoped是什么
  • 二、应用案例
    • 1.使用代码
    • 2.原理
    • 3父组件App未添加scoped影响


一、scoped是什么

我们知道vue为了防止css样式污染,在每个组件中提供了 scoped属性进行限定css作用域;当<style>标签有 scoped 属性时,它的 CSS 只作用于当前组件中的元素。

  1. 作用:让样式在局部生效,防止冲突。
  2. 写法:<style scoped>

二、应用案例

1.使用代码

School.vue

<template><div class="demo"><h2 class="title">学校名称:{{name}}</h2><h2>学校地址:{{address}}</h2></div>
</template><script>export default {name:'School',data() {return {name:'vue学院',address:'上海黄浦区',}}}
</script><style scoped>.demo{background-color: skyblue;}
</style>

Student.vue

<template><div class="demo"><h2 class="title">学生姓名:{{name}}</h2><h2 class="sex">学生性别:{{sex}}</h2></div>
</template><script>export default {name:'Student',data() {return {name:'张三',sex:'男'}}}
</script><style lang="less" scoped>.demo{background-color: pink;.sex{font-size: 50px;background: red;}}
</style>

App.vue

<template><div><h2 class="title">hello world</h2><School></School><hr><Student></Student></div>
</template><script>import Student from './components/Student.vue';import School from './components/School.vue';export default {name:'App',components:{Student,School},data() {return {}},}
</script><style scoped>.title{color: red;}
</style>

2.原理

在这里插入图片描述
我们可以看到,各自组件加上scoped属性之后,对应的样式标签元素会随机生成一个
[data-v-2232xxxx]这样的属性,来保证每个组件样式互不干扰。
代码如下(示例):

3父组件App未添加scoped影响

在这里插入图片描述
在这里插入图片描述
父组件的样式会应用到子组件上


关键字:商业网_站长之家官网查询_网站优化与seo_许昌seo推广

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: