35 在Vue3中如何通过SCSS编写样式

概述

在Vue3中通过SCSS编写样式并不难,这节课我们来简单的学习一下。

本节课需要依赖一个第三方库,我们先安装:

bash 复制代码
yarn add sass -D

基本用法

我们创建src/components/Demo35.vue,代码如下:

html 复制代码
<template>
  <button>这是一个按钮</button>
</template>
<style lang="scss">
button {
  color: red;
  width: 100px;
  height: 30px;
  padding: 5px;
}
</style>

接着,我们修改src/App.vue:

html 复制代码
<script setup>
import Demo from "./components/Demo35.vue"
</script>
<template>
  <h1>欢迎跟着Python私教一起学习Vue3入门课程</h1>
  <hr>
  <demo/>
</template>

然后,我们浏览器访问:http://localhost:5173/

完整代码

package.json

json 复制代码
{
  "name": "hello",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview"
  },
  "dependencies": {
    "countable": "^3.0.1",
    "vue": "^3.3.8"
  },
  "devDependencies": {
    "@vitejs/plugin-vue": "^4.5.0",
    "sass": "^1.69.5",
    "vite": "^5.0.0"
  }
}

vite.config.js

js 复制代码
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

export default defineConfig({
  plugins: [vue()],
})

index.html

html 复制代码
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" type="image/svg+xml" href="/vite.svg" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Vite + Vue</title>
  </head>
  <body>
    <div id="app"></div>
    <script type="module" src="/src/main.js"></script>
  </body>
</html>

src/main.js

js 复制代码
import { createApp } from 'vue'
import App from './App.vue'

createApp(App).mount('#app')

src/App.vue

html 复制代码
<script setup>
import Demo from "./components/Demo35.vue"
</script>
<template>
  <h1>欢迎跟着Python私教一起学习Vue3入门课程</h1>
  <hr>
  <demo/>
</template>

src/components/Demo35.vue

html 复制代码
<template>
  <button>这是一个按钮</button>
</template>
<style lang="scss">
button {
  color: red;
  width: 100px;
  height: 30px;
  padding: 5px;
}
</style>

启动方式

bash 复制代码
yarn
yarn dev

浏览器访问:http://localhost:5173/

相关推荐
AsiaLYF几秒前
Kotlin MutableSharedFlow: emit vs tryEmit 详解
开发语言·前端·kotlin
喜欢踢足球的老罗几秒前
Chrome MV3 插件架构深度解析:Service Worker 生命周期与 Token 管理的三层博弈
前端·chrome·架构
小李云雾3 分钟前
Pinia:Vue3 全局状态管理从入门到精通
前端·javascript·vue.js
Upsy-Daisy8 分钟前
Hermes Agent 学习笔记 03:CLI 与 TUI 使用体验,让 Agent 真正进入终端工作流
服务器·前端·数据库
KaMeidebaby9 分钟前
卡梅德生物技术快报|噬菌体筛选:技术实操:宽谱大肠杆菌噬菌体筛选全流程与性能验证方案
前端·人工智能·算法·数据挖掘·数据分析
风吹夏回12 分钟前
Vue3 + Element Plus 完整使用指南
前端·javascript·vue.js·element
影寂ldy14 分钟前
C# 泛型方法
java·前端·c#
依托偶尔宁14 分钟前
element-plus:el-table设置展开图标所在列的位置
前端·elementui
小小龙学IT16 分钟前
Go语言后端开发实战指南:构建高性能云原生服务
前端·云原生·golang
sbjdhjd9 小时前
Redis 主从复制、哨兵高可用与 Cluster 集群部署实验手册
运维·前端·redis·云原生·开源·bootstrap·html