search
HomeWeb Front-endVue.jsSimple analysis of the role of vue component scoped

This article brings you relevant knowledge about vue, which mainly introduces issues related to the scoped attribute. If you add the scoped attribute to the style, you will add it to the label of this component. Let’s take a look at the attributes starting with the last randomly generated data-v. I hope it will be helpful to everyone.

Simple analysis of the role of vue component scoped

[Related recommendations: javascript video tutorial, vue.js tutorial

vue component-scoped Function

Purpose: Solve the conflict problem of multiple components with the same style name

Requirements: p tag name selector, set background color

Problem: It is found that both the p in the component and the p outside are effective

Solution: Add the scoped attribute to the style tag in the Panel.vue component

<style></style>
// style 添加 scoped 属性后,可以实现样式独立作用域<style>p{
    background-color: pink;}</style>

Add the scoped attribute to the style, and it will Add a randomly generated attribute starting with data-v to the label of this component

and it must be an element of the current component to have this custom attribute and be affected by this style

Simple analysis of the role of vue component scoped

Summary: Add scoped to style, and the style in the component only takes effect in the current vue component

The mission and role of scoped

Here is a demo made with vue. There are two pages. One page is the a tag of the Nuggets, and the other page is the a tag of vue. Let’s take a look at the role of scoped and give the a tag to one of the pages. Add red, and the other one does not add any style, and does not use scoped in the style, as shown below

Simple analysis of the role of vue component scoped

Everyone should know the answer, yes! The link text in both pages turns red. When we click on the juejin page, the browser will automatically introduce the style of the current page. When we click on github again, the browser will reuse this style, so the color of the link a will all turn red, because Vue is a single-page application, so the styles of every page we open will be sandwiched into global styles. The solution is to add scoped. At this time, the color of the a tag will be different. Let’s open the page to see Next a tag (picture below)

Simple analysis of the role of vue component scoped

At this time, the a tag has an extra data-v-xxxxxxx, which Vue adds for us As above, when Vue builds this project, it will remind us to use component units. All elements under different components will be added with the same component ID, and at the same time, attribute selectors will be used in the styles we write. After splicing this unique identifier, let’s verify it, (picture below)

Simple analysis of the role of vue component scoped

Yes, after we add scoped, Vue will be in this style Add this attribute to the last paragraph, and then select the element through the attribute selector to isolate the element style. Next, let’s take a look at the side effects of scoped

The side effects of scoped

At this time We introduce element-ui to verify the side effects of scoped. Here is a demo (picture below). Scoped is also used here

Simple analysis of the role of vue component scoped
Note that there is no input element here. For example, data-v- The unique identifier of xxxxxx is

. But what does the style we wrote ourselves (my-Txt input) look like after it is constructed? (Picture below)

Simple analysis of the role of vue component scoped

#As shown in the picture above, the style we wrote ourselves has been spliced ​​with this unique identifier, so no matter how we operate, we cannot hit this element. , that is to say, Vue did not add this logo to this input, but added this logo to our style. However, if we remove scoped, we can hit the target, but we do not recommend this. Style Will be packaged into a unified style. This is a side effect of scoped. So how do we solve it? Style penetration

<style>
.my-Txt {
  width: 200px;
}
.my-Txt ::v-deep input {
  background-color: pink;
}
</style>

Simple analysis of the role of vue component scoped
##

We can see that this unique identifier runs from the back of the input to the back of my-Txt, which means that if we don’t add this::v-deep, Vue will always splice this unique identifier into the style we wrote. In the last paragraph, if we want to change the position of this unique identifier, we need to specify it with style penetration. Then the above picture means that the my-Txt element with this unique identifier will be applied to the input in its child elements. This style, and the my-Txt we wrote also has this unique identifier, as you can see from the picture above.

【Related recommendations: javascript video tutorial, vue.js tutorial

The above is the detailed content of Simple analysis of the role of vue component scoped. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
What Happens When the Vue.js Virtual DOM Detects a Change?What Happens When the Vue.js Virtual DOM Detects a Change?May 14, 2025 am 12:12 AM

WhentheVue.jsVirtualDOMdetectsachange,itupdatestheVirtualDOM,diffsit,andappliesminimalchangestotherealDOM.ThisprocessensureshighperformancebyavoidingunnecessaryDOMmanipulations.

How Accurate Is It to Think of Vue.js's Virtual DOM as a Mirror of the Real DOM?How Accurate Is It to Think of Vue.js's Virtual DOM as a Mirror of the Real DOM?May 13, 2025 pm 04:05 PM

Vue.js' VirtualDOM is both a mirror of the real DOM, and not exactly. 1. Create and update: Vue.js creates a VirtualDOM tree based on component definitions, and updates VirtualDOM first when the state changes. 2. Differences and patching: Comparison of old and new VirtualDOMs through diff operations, and apply only the minimum changes to the real DOM. 3. Efficiency: VirtualDOM allows batch updates, reduces direct DOM operations, and optimizes the rendering process. VirtualDOM is a strategic tool for Vue.js to optimize UI updates.

Vue.js vs. React: Scalability and MaintainabilityVue.js vs. React: Scalability and MaintainabilityMay 10, 2025 am 12:24 AM

Vue.js and React each have their own advantages in scalability and maintainability. 1) Vue.js is easy to use and is suitable for small projects. The Composition API improves the maintainability of large projects. 2) React is suitable for large and complex projects, with Hooks and virtual DOM improving performance and maintainability, but the learning curve is steeper.

The Future of Vue.js and React: Trends and PredictionsThe Future of Vue.js and React: Trends and PredictionsMay 09, 2025 am 12:12 AM

The future trends and forecasts of Vue.js and React are: 1) Vue.js will be widely used in enterprise-level applications and have made breakthroughs in server-side rendering and static site generation; 2) React will innovate in server components and data acquisition, and further optimize the concurrency model.

Netflix's Frontend: A Deep Dive into Its Technology StackNetflix's Frontend: A Deep Dive into Its Technology StackMay 08, 2025 am 12:11 AM

Netflix's front-end technology stack is mainly based on React and Redux. 1.React is used to build high-performance single-page applications, and improves code reusability and maintenance through component development. 2. Redux is used for state management to ensure that state changes are predictable and traceable. 3. The toolchain includes Webpack, Babel, Jest and Enzyme to ensure code quality and performance. 4. Performance optimization is achieved through code segmentation, lazy loading and server-side rendering to improve user experience.

Vue.js and the Frontend: Building Interactive User InterfacesVue.js and the Frontend: Building Interactive User InterfacesMay 06, 2025 am 12:02 AM

Vue.js is a progressive framework suitable for building highly interactive user interfaces. Its core functions include responsive systems, component development and routing management. 1) The responsive system realizes data monitoring through Object.defineProperty or Proxy, and automatically updates the interface. 2) Component development allows the interface to be split into reusable modules. 3) VueRouter supports single-page applications to improve user experience.

What are the disadvantages of VueJs?What are the disadvantages of VueJs?May 05, 2025 am 12:06 AM

The main disadvantages of Vue.js include: 1. The ecosystem is relatively new, and third-party libraries and tools are not as rich as other frameworks; 2. The learning curve becomes steep in complex functions; 3. Community support and resources are not as extensive as React and Angular; 4. Performance problems may be encountered in large applications; 5. Version upgrades and compatibility challenges are greater.

Netflix: Unveiling Its Frontend FrameworksNetflix: Unveiling Its Frontend FrameworksMay 04, 2025 am 12:16 AM

Netflix uses React as its front-end framework. 1.React's component development and virtual DOM mechanism improve performance and development efficiency. 2. Use Webpack and Babel to optimize code construction and deployment. 3. Use code segmentation, server-side rendering and caching strategies for performance optimization.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools