使用 scoped 后,父組件的樣式將不會滲透到子組件中。
例如(無效):
<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/
<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>
官方文檔:https://vue-loader.vuejs.org/guide/scoped-css.html#deep-selectors
轉載自 https://wolfx.cn/vue-scoped-css-questions/
