如何在Vue.js中使用/ deep /或>>>?

lap*_*tou 24 css sass node.js webpack vue.js

所以,我在这里读到,在Vue.js中,您可以使用/deep/>>>在选择器中创建适用于子组件内部元素的样式规则.但是,尝试在我的样式中使用它,无论是在SCSS中还是在普通的旧CSS中,都不起作用.相反,它们会逐字发送到浏览器,因此无效.例如:

home.vue:

<style lang="css" scoped>
    .autocomplete >>> .autocomplete-input
    {
    // ...
    }
</style>
Run Code Online (Sandbox Code Playgroud)

生成的css:

<style type="text/css">
.autocomplete >>> .autocomplete-input[data-v-2bda0c9a]
{
//...
}
</style>
Run Code Online (Sandbox Code Playgroud)

我想要的是:

<style type="text/css">
.autocomplete[data-v-2bda0c9a] .autocomplete-input
{
//...
}
</style>
Run Code Online (Sandbox Code Playgroud)

我的webpack配置vue-loader如下所示:

// ...
{
    test: /\.vue$/,
    loader: "vue-loader",
    options: {
        loaders: {
            scss: "vue-style-loader!css-loader!sass-loader"
        }
    }
}
// ...
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,如何使这个>>>运算符工作?

我已经找到了这个答案,但我正是这样做而且它不起作用......

Dan*_*Dan 36

使用:: v-deep

可接受的答案在范围内的SCSS中对我不起作用,但这有时可以做到:

.parent-class {
  &::v-deep .child-class {
    background-color: #000;
  }
}
Run Code Online (Sandbox Code Playgroud)

通常这样做(省略父类):

::v-deep .child-class {
  background-color: #000;
}
Run Code Online (Sandbox Code Playgroud)

编辑(10/1/2019):额外信息

  • sass并且dart-sass不支持/deep/,只有node-sass
  • Vuetify 2不支持/deep/(因为它不支持node-sass

  • 现在 Vuetify 2 已经发布,您需要确保不要使用 `/deep/` 而是使用 `::v-deep`,因为 `sass` 和 `dart-sass` 都不支持 `/deep/ `。`node-sass` 可以,但 Vuetify 不能与 `node-sass` 一起使用。 (4认同)
  • 如果您使用的是dart-sass,那么这将是正确的方法,因为dart-sass不知道如何解析/ deep /组合器。 (2认同)
  • 确认 `:deep()` 语法在 Vue 2.7 中有效 (2认同)

ara*_*l3y 21

我已成功使用/ deep /在Vue的SCSS样式表中使用此结构:

.parent-class {
  & /deep/ .child-class {
    background-color: #000;
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 请检查其他答案“/deep/”已被弃用! (5认同)

Sat*_*hak 11

component's可以使用来更改任何作用域的CSS,deep selector 但是/deep/将不建议使用

Vue Github参考-https: //github.com/vuejs/vue-loader/issues/913

::v-deep在这种情况下使用,并避免不推荐使用/deep/

参考- 深度选择器

只需检查element您要devtools在chrome或任何浏览器控制台中使用的渲染类的类即可。

然后,在您消费时component,对其进行修改

<style scoped>
::v-deep .custom-component-class {
   background: red; //
}
</style>
Run Code Online (Sandbox Code Playgroud)


小智 8

::v-deep 作为组合器的用法已被弃用。使用 :deep() 代替。

:deep(.child-class) {
    background-color: #000;
}
Run Code Online (Sandbox Code Playgroud)

  • 如何在多个类中使用它? (4认同)