Skip to content

[vue] 怎么使css样式只在当前组件中生效? #258

Open
@haizhilin2013

Description

@haizhilin2013
Collaborator

[vue] 怎么使css样式只在当前组件中生效?

Activity

wenyejie

wenyejie commented on Jun 17, 2019

@wenyejie

scoped

April-Zheng

April-Zheng commented on Jul 1, 2019

@April-Zheng

scoped

ccllff

ccllff commented on Jul 4, 2019

@ccllff

scoped

CaoRuimiao

CaoRuimiao commented on Jul 14, 2019

@CaoRuimiao
<style scoped> </style>
Ricemonster

Ricemonster commented on Jul 26, 2019

@Ricemonster

scoped

qp97vi

qp97vi commented on Aug 7, 2019

@qp97vi

在style上面写 scoped

zejunking

zejunking commented on Aug 19, 2019

@zejunking

css module

Elisony

Elisony commented on Sep 26, 2019

@Elisony
<style scoped> </style>
crush2020

crush2020 commented on Feb 1, 2021

@crush2020
<style scoped></style>
yxllovewq

yxllovewq commented on Mar 9, 2022

@yxllovewq

在组件.vue的style标签加上scoped属性

AAA611

AAA611 commented on Aug 30, 2022

@AAA611

1

scoped

2

css module

3

css in js

Elisony

Elisony commented on Aug 30, 2022

@Elisony
sc950828

sc950828 commented on Sep 23, 2022

@sc950828
<style scoped>或者<style module>
Elisony

Elisony commented on Sep 23, 2022

@Elisony
Cai-zhiji

Cai-zhiji commented on Jul 5, 2023

@Cai-zhiji

作用域样式(Scoped Styles):

在单文件组件(.vue 文件)中使用 <style> 标签,并为其添加 scoped 属性。

<style scoped>
/* 在这里编写组件的样式 */
</style>

使用作用域样式后,样式规则将被自动添加一个唯一的属性选择器,以确保样式仅应用于当前组件中的元素。

CSS 模块(CSS Modules)

在单文件组件中使用 <style> 标签,并将其设置为 module。

<style module>
/* 在这里编写组件的样式 */
</style>

使用 CSS 模块后,样式将被自动转换为局部作用域,并生成一个唯一的类名,只在当前组件中有效。在组件的模板中,可以通过绑定类名的方式使用这些样式。

<template>
  <div :class="$style.myComponentStyle">...</div>
</template>
Elisony

Elisony commented on Jul 5, 2023

@Elisony
WangLiqing0713

WangLiqing0713 commented on Jul 11, 2023

@WangLiqing0713

< style scoped></style>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @wenyejie@haizhilin2013@CaoRuimiao@zejunking@ccllff

        Issue actions

          [vue] 怎么使css样式只在当前组件中生效? · Issue #258 · haizlin/fe-interview