Vue3 中 <style scoped> 的作用和用法
<p>在 Vue3 中,`<style scoped>` 表示该样式仅在当前组件中生效,不会影响到父组件和子组件。这是通过将样式中的类名和 ID 自动添加一个唯一的前缀来实现的,从而避免了全局样式的污染。</p>
<p>例如:</p>
<pre><code class="language-html"><template>
<div class='example'>
<h1>Example Component</h1>
<p>This is an example component with scoped styles.</p>
</div>
</template>
<style scoped>
.example h1 {
color: red;
}
.example p {
font-size: 16px;
}
</style>
</code></pre>
<p>在上面的例子中,该组件中的样式只会应用于包含 `<div class='example'>` 的元素,不会影响到其他组件。</p>
原文地址: https://www.cveoy.top/t/topic/oPZh 著作权归作者所有。请勿转载和采集!