唯品秀前端博客

使用 scoped 后,父组件的样式将不会渗透到子组件中。

例如(无效):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<template>
  <div id="app">
    <el-input  class="text-box" v-model="text"></el-input>
  </div>
</template>

<script>
export default {
  name: 'App',
  data() {
    return {
      text: 'hello'
    };
  }
};
</script>

<style lang="less" scoped>
.text-box {
   input {
    width: 166px;
    text-align: center;
  }
}
</style>

解决方法:

使用深度作用选择器 /deep/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<template>
  <div id="app">
    <el-input v-model="text" class="text-box"></el-input>
  </div>
</template>

<script>
export default {
  name: 'App',
  data() {
    return {
      text: 'hello'
    };
  }
};
</script>

<style lang="less" scoped>
.text-box {
  /deep/ input {
    width: 166px;
    text-align: center;
  }
}
</style>

简便方式

1
2
3
4
<style scoped>
.a >>> .b { /* ... */ }
</style>
//对于scss这类的预处理器需要用上面/deep/的写法,此方法无法正常编译解析

以上将编译成:

1
.a[data-v-f3f3eg9] .b { /* ... */ }

官方文档:https://vue-loader.vuejs.org/guide/scoped-css.html#deep-selectors

本站所有文章、图片、资源等如无特殊说明或标注,均为来自互联网或者站长原创,版权归原作者所有;仅作为个人学习、研究以及欣赏!如若本站内容侵犯了原著者的合法权益,可联系我们进行处理,邮箱:343049466@qq.com
赞(10) 打赏
标签:

上一篇:

下一篇:

相关推荐

0 条评论关于"Vue scoped CSS 与深度作用选择器 deep"

表情

最新评论

    暂无留言哦~~
谢谢你请我吃鸡腿*^_^*

支付宝扫一扫打赏

微信扫一扫打赏