Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

css - How do I use /deep/ or >>> or ::v-deep in Vue.js?

So, I've read here that in Vue.js, you can use /deep/ or >>> in a selector in order to create style rules that apply to elements inside of child components. However, attempting to use this in my styles, whether in SCSS or in plain old CSS, doesn't work. Instead, they are sent to the browser verbatim, and therefore have no effect. For example:

home.vue:

<style lang="css" scoped>
    .autocomplete >>> .autocomplete-input
    {
    // ...
    }
</style>

generated css:

<style type="text/css">
.autocomplete >>> .autocomplete-input[data-v-2bda0c9a]
{
//...
}
</style>

what I want:

<style type="text/css">
.autocomplete[data-v-2bda0c9a] .autocomplete-input
{
//...
}
</style>

My webpack configuration pertaining to vue-loader looks like this:

// ...
{
    test: /.vue$/,
    loader: "vue-loader",
    options: {
        loaders: {
            scss: "vue-style-loader!css-loader!sass-loader"
        }
    }
}
// ...

So my question is, how do I get this >>> operator to work?

I've already found this answer, but I'm doing exactly that and it doesn't work...

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Vue 2

The following also works in Vue 3 but is deprecated.

Sass: ??Use ::v-deep

::v-deep .child-class {
    background-color: #000;
}

Not using Sass: ??Use >>>

>>> .child-class {
    background-color: #000;
}

With either syntax, the <style> tag for this component must be scoped:

<style scoped>

Vue 3

In Vue 3, we no longer need >>> and can use the unified ::v-deep selector whether using Sass or not, but now it's recommended to use parentheses with the selector.

Use ::v-deep(.child-class)

::v-deep(.child-class) {
    background-color: #000;
}

You can also still use any of the deprecated syntaxes if you prefer.


Vue 3 new selectors

Additionally, in Vue 3, there is a new syntax for components with a <slot> that enables styling passed slot content.

Slot content ??Use ::v-slotted(.child-class)

::v-slotted(.slot-class) {
    background-color: #000;
}

And lastly, in Vue 3, there is a new syntax to register global styles even from a scoped component:

Global styles ??Use ::v-global(.my-class)

::v-global(.my-class) {
    background-color: #000;
}

With any syntax, the <style> tag for this component must be scoped:

<style scoped>

Summary

In Vue 2:

  • The /deep/ syntax is deprecated
  • Use ::v-deep with Sass, use >>> without Sass

In Vue 3:

  • ::v-deep .child-class is deprecated in favor of ::v-deep(.child-class)
  • The >>> syntax is deprecated
  • The /deep/ syntax is deprecated
  • There are new ::v-slotted and ::v-global selectors

With every version/syntax, the <style> tag for this component must be scoped:

<style scoped>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...