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/

相关推荐
一 乐18 小时前
幼儿园管理|基于springboot + vue幼儿园管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端
JH灰色18 小时前
【大模型】-LangChain多模态输入和自定义输出
java·前端·langchain
JIngJaneIL18 小时前
基于Java + vue校园论坛系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
馬致远19 小时前
Vue TodoList 待办事项小案例(代码版)
前端·javascript·vue.js
一字白首19 小时前
Vue 进阶,Vuex 核心概念 + 项目打包发布配置全解析
前端·javascript·vue.js
栀秋66619 小时前
从前端送花说起:HTML敲击乐与JavaScript代理模式的浪漫邂逅
前端·javascript·css
刘同学有点忙19 小时前
国际化语言包与Excel自动化双向转换方案
前端
bm90dA19 小时前
前端小记:Vue3引入mockjs开发
前端
渔_19 小时前
SCSS 实战指南:从基础到进阶,让 CSS 编写效率翻倍
前端
Syron19 小时前
为什么微应用不需要配置 try_files?
前端