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

概述

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

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

yarn add sass -D

基本用法

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

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

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

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

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

在这里插入图片描述

完整代码

package.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

import {
    defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

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

index.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

import {
    createApp } from 'vue'
import App from './App.vue'

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

src/App.vue

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

src/components/Demo35.vue

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

启动方式

yarn
yarn dev

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

相关推荐

  1. 10 Vue3使用SCSS编写样式

    2023-12-21 17:04:03       31 阅读
  2. vue3集成sass实现全局scss样式变量

    2023-12-21 17:04:03       34 阅读
  3. 【无标题】Vue3scss想使用动态的变量

    2023-12-21 17:04:03       29 阅读
  4. vue3vite使用sass

    2023-12-21 17:04:03       40 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-21 17:04:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-21 17:04:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-21 17:04:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-21 17:04:03       20 阅读

热门阅读

  1. 【密码学引论】密码协议

    2023-12-21 17:04:03       30 阅读
  2. 在Idea中创建基于工件的本地服务

    2023-12-21 17:04:03       42 阅读
  3. Codeforces Round 916 (Div. 3)(A~E2)

    2023-12-21 17:04:03       37 阅读
  4. 深度学习嵌入头embedding head解释

    2023-12-21 17:04:03       38 阅读
  5. 7-1 冰雹猜想

    2023-12-21 17:04:03       43 阅读
  6. minio 整合springboot

    2023-12-21 17:04:03       30 阅读
  7. Springboot Async 引起的循环依赖

    2023-12-21 17:04:03       37 阅读
  8. 云服务器的优缺点对比

    2023-12-21 17:04:03       36 阅读
  9. 第十五章 Linux系统日志管理

    2023-12-21 17:04:03       24 阅读