css - :first-child 样式未在 Vue 组件中应用
问题描述
问题:
通常第一个<User>
应该有margin-left: 0px
并且应该与其他元素对齐到左边,但这不起作用,正如您在下面的屏幕截图中看到的那样。有人知道如何正确解决这个问题吗?
sidebar.scss
代码:
.user {
margin-left: -8px;
&:first-child {
margin-left: 0;
}
}
Sidebar.vue
代码
<template>
<section class="sidebar">
<slot></slot>
</section>
</template>
<script>
export default {
name: "Sidebar"
}
</script>
<style scoped lang="scss">
@import 'sidebar';
</style>
Profile.vue
代码
<template>
<main>
<Sidebar>
<Header text="Members" :button="{text: 'Edit'}" hasBorder="true"></Header>
<User img="..."></User>
<User img="..."></User>
<User img="..."></User>
<User img="..."></User>
<Button color="grey" isRounded="true" isIconOnly="true"></Button>
</Sidebar>
</main>
</template>
User.vue
代码:
<template>
<div class="user">
<img v-if="this.img" :src="this.img" :alt="this.name">
<div v-if="!this.img"></div>
<h6 v-if="this.name">{{this.name}}</h6>
<slot></slot>
</div>
</template>
解决方案
用于::v-deep
更改scoped
css。
我已经回答了这个问题,但我有一个旧的和更具描述性的答案,这可能有助于您了解用法https://stackoverflow.com/a/56698470/7358308
https://vue-loader.vuejs.org/guide/scoped-css.html#deep-selectors
注意:不要使用/deep/
,因为它已被弃用,并且当您使用预处理器时,>>>
组合器也不起作用sass
::v-deep {
.user {
margin-left: -8px;
&:first-child {
margin-left: 0;
}
}
}
推荐阅读
- excel - VBA 代码:Excel 范围未正确粘贴到 Outlook
- junit - 使用 JUnitCore 运行时,TestWatcher 不会捕获失败的测试
- c - 找不到如何正确使用 ptrace()
- python - 无法在 Windows 上使用键盘 Ctrl+C 中断线程化 Python 控制台应用程序
- java - 通过 selenium 禁用 chrome 中的文本自动填充
- enums - 解构枚举时是否可以向上转换为特征?
- python - 如何使用 POST GET 传递 access_token
- r - 使用 dplyr 有条件地将一列中的值替换为另一列中的值
- json - 尝试使用文件加载器和 webpack 加载大型 json 文件
- css - 如何在 Bootstrap 4 中居中对齐文本