10 在Vue3中使用SCSS编写样式

概述

When using Vue components, the Vite compiler allows you to use almost any frontend templating language style. The easiest way to enable these expressive library plugins in your Vue templates is to install them when you initialize your project, or by using npm install (or yarn add) for the package.

使用 Vue 组件时,Vite 编译器允许您使用几乎任何前端模板语言风格。在 Vue 模板中启用这些表现力库插件的最简单方法是在初始化项目时安装它们,或使用 npm install(或 yarn add)安装包。

When using the style tag inside of a Vue component, you can specify a language using the lang attribute, provided that you have installed that specific language plugin.

在 Vue 组件中使用样式标签时,只要安装了特定的语言插件,就可以使用 lang 属性指定语言。

For example, if you chose to install the Stylus preprocessor, first you need to install the stylus package in your project as a dependency by performing the following command:

例如,如果您选择安装 Stylus 预处理器,首先需要执行以下命令将 stylus 软件包作为依赖关系安装到项目中:

npm add -D stylus
#OR
yarn add -d stylus

Then, you can add the lang=“stylus” attribute to the style tag to begin using Stylus:

然后,就可以在样式标签中添加 lang=“stylus” 属性,开始使用 Stylus:

<style lang="stylus">
ulcolor: #2c3e50;> h2color: #22cc33;
</style>

Another benefit of using Vue is scoping the style with the scoped attribute. This is a useful way to create isolated and component-specific CSS stylings. It also overrides any other CSS global rules, according to the CSS rule of specificity.

使用 Vue 的另一个好处是使用 scoped 属性对样式进行定义。这是一种有用的方法,可用于创建孤立的、特定于组件的 CSS 样式。根据 CSS 的特定性规则,它还可以覆盖任何其他 CSS 全局规则。

It is not recommended to scope global styles. A common method for defining global styling is to separate these styles into another style sheet and import them into your App.vue file.

不建议对全局样式设置范围。定义全局样式的常用方法是将这些样式分离到另一个样式表中,然后导入到 App.vue 文件中。

Now, let’s practice importing SCSS, a pre-processor plugin for CSS, to use in your application, and write some scoped stylings with the following exercise.

现在,让我们练习导入 SCSS(CSS 的预处理器插件)到应用程序中,并通过下面的练习编写一些范围样式。

练习:使用SCSS

In this exercise, we will be utilizing the style tag to add SCSS preprocessed styles to a component and importing external stylesheets.

在本练习中,我们将利用样式标签为组件添加 SCSS 预处理样式,并导入外部样式表。

Create a new Vue component file named Exercise1-11.vue in the src/components directory.

在 src/components 目录中新建一个名为 Exercise1-11.vue 的 Vue 组件文件。

在App.vue中引入该组件并渲染:

<script setup>
import Exercise from "./components/Exercise1-11.vue";
</script>
<template><Exercise/>
</template>

Inside Exercise1-11.vue, let’s write some HTML that can be styled using SCSS. Let’s keep practicing the interpolation method:

在 Exercise1-11.vue 中,让我们编写一些可以使用 SCSS 定型的 HTML。让我们继续练习插值法:

<template><div><h1>{{ title }}</h1><h2>{{ subtitle }}</h2><ul><li>{{ items[0] }}</li><li>{{ items[1] }}</li><li>{{ items[2] }}</li></ul></div>
</template>
<script>
export default {data() {return {title: 'My list component!',subtitle: 'Vue JS basics',items: ['Item 1', 'Item 2', 'Item 3']}},
}
</script>

Add the sass SCSS package as a project dependency:

将 sass SCSS 软件包添加为项目依赖关系:

npm install -D sass# 或者
npm add -D sass# 或者
yarn add sass

Add the lang attribute to the style tag and add the scss value to enable SCSS syntax inside the style block:

在样式标签中添加 lang 属性,并添加 scss 值,以便在样式块中启用 SCSS 语法:

<style lang="scss"></style>

Create a folder inside the src/ directory called styles. Inside this new folder, create a file called typography.scss.

在 src/ 目录下创建一个名为 styles 的文件夹。在这个新文件夹中,创建一个名为 typography.scss 的文件。

Inside typography.scss, add some styling for the template you composed in your component, such as defining color variables (green, grey, and blue) to reuse in different areas of related CSS rules, and some CSS styles for h1, h2, and the list elements:

在 typography.scss 中,为组件中的模板添加一些样式,例如定义颜色变量(绿色、灰色和蓝色),以便在相关 CSS 规则的不同区域重复使用,以及为 h1、h2 和列表元素添加一些 CSS 样式:

/* typography.scss */
$color-green: #4fc08d;
$color-grey: #2c3e50;
$color-blue: #003366;
h1 {margin-top: 60px;text-align: center;color: $color-grey;+ h2 {text-align: center;color: $color-green;}
}ul {display: block;margin: 0 auto;max-width: 400px;padding: 30px;border: 1px solid rgba(0, 0, 0, 0.25);> li {color: $color-grey;margin-bottom: 4px;}
}

In SCSS, you can use standard CSS selectors to select elements in your component.

在 SCSS 中,您可以使用标准 CSS 选择器来选择组件中的元素。

ul > li will select every <li> element inside of an <ul> element for styling. Similarly, using the addition symbol (+) means that the elements placed after the first element will be styled if they match the condition. For example, h1 + h2 will dictate that all h2 elements after h1 will be styled in a certain way, but h3 will not. You can understand this better through the following example.

ul > li 将选择 <ul> 元素内的每个 <li> 元素进行样式设置。同样,使用加号 (+) 表示,如果第一个元素之后的元素符合条件,那么这些元素就会被样式化。例如,h1 + h2 表示 h1 之后的所有 h2 元素都将以某种方式进行样式调整,但 h3 则不会。通过下面的示例,您可以更好地理解这一点。

In CSS, you would present this code as follows:

在 CSS 中,您可以使用如下代码来呈现这段代码:

h1 + h2 {
/* Add styling */
}
ul > li {
/* Add styling */
}

In SCSS, the same code can be represented as follows:

在 SCSS 中,同样的代码可以表示如下:

h1 {+ h2 {// Add styling}
}
ul {> li {// Add styling}
}

In your component, import these styles by using the SCSS @import method:

在组件中,使用 SCSS @import 方法导入这些样式:

<style lang="scss">
@import '../styles/typography.scss';
</style>

Add the scoped attribute to your <style> tag to only apply these styles to this component instance. Use the variable from the $color-blue imported stylesheet:

<style> 标签中添加作用域属性,以便仅将这些样式应用于该组件实例。使用从 $color-blue 导入的样式表中的变量:

<style lang="scss" scoped>
@import '../styles/typography';
h1 {font-size: 50px;color: $color-blue; // Use variables from imported stylesheets
}
</style>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/227969.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

在Node.js中MongoDB的连接查询操作

本文主要介绍在Node.js中MongoDB的连接查询操作。 目录 Node.js中MongoDB的连接查询操作使用原生的mongodb驱动程序进行连接查询操作使用Mongoose库进行连接查询操作注意项 Node.js中MongoDB的连接查询操作 在Node.js中使用MongoDB进行连接操作&#xff0c;可以使用原生的mong…

条款27:尽量少做转型动作

1.前言 C规则的设计目标之一是保证“类型错误”绝对不可能发生。理论上如果你的程序很顺利的通过编译&#xff0c;就表示它并不企图在任何对象身上执行任何不安全&#xff0c;无意义的操作。这是个极具价值的保证&#xff0c;可别草率的放弃它。 不幸的是&#xff0c;转型&am…

AOP切入点表达式和使用连接点获取匹配到的方法信息

目录 第一种 execution(访问修饰符? 返回值 包名.类名.?方法名(方法参数) throws 异常?) 第二种 annotation(com.itheima.anno.Log 首先&#xff0c;自定义一个注解&#xff0c;可以自己随意命名&#xff1a; 第一种 execution(访问修饰符? 返回值 包名.类名.?方法名…

Explain工具-SQL性能优化

文章目录 SQL性能优化的目标Explain中type效率级别&#xff08;重要&#xff09;注意 Explain覆盖索引ExplainindexExplainfilesortExplainfilesort创建 idx_bd(b,d) SQL性能优化的目标 达到 range 级别 Explain中type效率级别&#xff08;重要&#xff09; 显示的是单位查询…

工作流JBPM笔记:了解JBPM

一、什么是工作流 工作流管理联盟&#xff08;WFMC&#xff09;把工作流定义为&#xff1a;全部或部分由计算机支持或自动处理的业务过程。 工作流管理系统&#xff08;Workflow Management System&#xff0c;WFMS&#xff09;用来支持流程定义、管理和执行一批设定好的工作…

pytorch强化学习(1)——DQNSARSA

实验环境 python3.10 torch2.1.1 gym0.26.2 gym[classic_control] matplotlib3.8.0 numpy1.26.2DQN代码 首先是module.py代码&#xff0c;在这里定义了网络模型和DQN模型 import torch import torch.nn as nn import numpy as npclass Net(nn.Module):# 构造只有一个隐含层的…

Qt容器QToolBox工具箱

# QToolBox QToolBox是Qt框架中的一个窗口容器类,常用的几个函数有: ​setCurrentIndex(int index):设置当前显示的页面索引。可以通过调用该函数,将指定索引的页面设置为当前显示的页面。 addItem(QWidget * widget, const QString & text):向QToolBox中添加一个页面…

Flink系列之:分组聚合

Flink系列之&#xff1a;分组聚合 一、DISTINCT 聚合二、GROUPING SETS三、ROLLUP四、CUBE五、HAVING 适用于流、批 像大多数数据系统一样&#xff0c;Apache Flink支持聚合函数&#xff1b;包括内置的和用户定义的。用户自定义函数在使用前必须在目录中注册。 聚合函数把多行…

flutter学习-day10-布局类组件

&#x1f4da; 目录 介绍布局原理和约束盒模型布局 约束容器ConstrainedBox非约束容器UnconstrainedBox 线性布局 行row列column 弹性布局流式布局 WrapFlow 层叠布局对齐和相对定位布局构建回调 LayoutBuilder布局过程中AfterLayout布局完成后执行 本文学习和引用自《Flutte…

LLM大语言模型(二):Streamlit 无需前端经验也能画web页面

目录 问题 Streamlit是什么&#xff1f; 怎样用Streamlit画一个LLM的web页面呢&#xff1f; 文本输出 页面布局 滑动条 按钮 对话框 输入框 总结 问题 假如你是一位后端开发&#xff0c;没有任何的web开发经验&#xff0c;那如何去实现一个LLM的对话交互页面呢&…

Python MySQL数据库连接与基本使用

一、应用场景 python项目连接MySQL数据库时&#xff0c;需要第三方库的支持。这篇文章使用的是PyMySQL库&#xff0c;适用于python3.x。 二、安装 pip install PyMySQL三、使用方法 导入模块 import pymysql连接数据库 db pymysql.connect(hostlocalhost,usercode_space…

Spring MVC开发流程

1.Spring MVC环境基本配置 Maven工程依赖spring-webmvc <dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.1.9.RELEASE</version> </dependency>web.xml配置Dispatche…

NSSCTF第16页(2)

[NSSRound#4 SWPU]1zweb(revenge) 查看index.php <?php class LoveNss{public $ljt;public $dky;public $cmd;public function __construct(){$this->ljt"ljt";$this->dky"dky";phpinfo();}public function __destruct(){if($this->ljt"…

【力扣100】73.矩阵置零

添加链接描述 class Solution:def setZeroes(self, matrix: List[List[int]]) -> None:"""Do not return anything, modify matrix in-place instead."""# 思路是1.记录每一个0元素的行和列下标 2.遍历全数组row_index[]column_index[]mlen(…

day01unittest复习,断言

1.unittest 方法执行前 # def setUp(self) -> None: # print(方法执行前执行) # # def tearDown(self) -> None: # print(方法执行后执行一次) 2.unittest 类方法执行前后执行一次 classmethod def setUpClass(cls) -> None:print(类执行前执行一次)classm…

41、BatchNorm - 什么是批归一化

在 CNN 网络中有一个很重要的技术,叫作批归一化(bn, BatchNorm )。 归一化层一般位于卷积的后面,学术或者工程上,一般习惯将卷积+批归一化+激活统一成一个小的网络结构,比如口语化上称为conv+bn+relu。 这是因为基本上卷积后面肯定会有批归一化,而后面肯定会接激活函数…

微分和导数(一)

1.微分&#xff1a; 假设我们有⼀个函数f : R → R&#xff0c;其输⼊和输出都是标量。如果f的导数存在&#xff0c;这个极限被定义为 如果f′(a)存在&#xff0c;则称f在a处是可微的。如果f在⼀个区间内的每个数上都是可微的&#xff0c;则此函数在此区间中是可微的。导数f′…

网络协议 - UDP 协议详解

网络协议 - UDP 协议详解 UDP概述UDP特点UDP的首部格式UDP校验 參考文章 基于TCP和UDP的协议非常广泛&#xff0c;所以也有必要对UDP协议进行详解。 UDP概述 UDP(User Datagram Protocol)即用户数据报协议&#xff0c;在网络中它与TCP协议一样用于处理数据包&#xff0c;是一种…

必要时进行保护性拷贝

保护性拷贝&#xff08;Defensive Copy&#xff09;是一种常见的编程实践&#xff0c;用于在传递参数或返回值时&#xff0c;创建副本以防止原始对象被意外修改。以下是一个例子&#xff0c;展示了何时进行保护性拷贝&#xff1a; mport java.util.ArrayList; import java.uti…

成功解决 Plugin ‘org.springframework.boot:spring-boot-maven-plugin:‘ not found

Plugin ‘org.springframework.boot:spring-boot-maven-plugin:‘ not found的解决方案&#xff0c;亲测可用&#xff01; 方法一&#xff1a;清理IDEA的缓存 File -> Invalidate Caches 方法二&#xff1a;添加版本号 先看自己当前的版本号 首先打开pom.xml文件进行查看C…