Home > Web Front-end > Vue.js > Style penetration in Vue3: deep() is invalid, how to solve it

Style penetration in Vue3: deep() is invalid, how to solve it

WBOY
Release: 2023-05-13 08:40:05
forward
2332 people have browsed it

: Usage scenarios of deep():

If the scoped attribute is added to the style node of the current component, the style of the current component will not take effect on its sub-components. If you want certain styles to take effect on child components, you need to use :deep().

I originally thought this was not difficult, so I wrote a case to verify it. Then the problem arises, the style defined by :deep() does not work in child components.
I started looking for errors and checked the grammatical format including colons and brackets. I found that there was no problem with the grammatical format and the console did not report an error. However, the style of the deep() format was invalid in the subcomponent. .
Fortunately, I have an example where the :deep() format works. I had no choice but to compare the parent components and sub-components in the two files one by one. After spending a lot of time, I finally found the problem.

When I was practicing earlier, Vue2.X required that elements must be in a root node. Vue3. The

was deleted and no error was reported.

Style penetration in Vue3: deep() is invalid, how to solve it

But the problem lies in this root node. If there is no such root node in App.vue, then: deep() will not work. I will add the root node. , the :deep() style will take effect.
Alas, I feel like I was tricked by this root node.

1. Code in parent component App.vue

<style lang="less" scoped>
  :deep(.title3){
    background-color:antiquewhite;
  }
</style>
Copy after login

2. Code in child component

<template>
  <h4 class="title3">受父组件影响的内容</h4>
</template>
Copy after login

The above is the detailed content of Style penetration in Vue3: deep() is invalid, how to solve it. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template