vite+vue3 css scss PC移动布局自适应

1. 安装 postcss-pxtorem 和 autoprefixer

npm install postcss-pxtorem autoprefixer --save

2. vite.config.js引入并配置

import postCssPxToRem from 'postcss-pxtorem'
import autoprefixer from 'autoprefixer'

export default defineConfig({
  base: './',
  resolve: {
    alias
  },
  plugins: [vue()],
  css: {
        postcss: { // 关键代码
            plugins: [
                postCssPxToRem({ // 自适应,px>rem转换
                    rootValue: 16, // 1rem的大小
                    propList: ['*'], // 需要转换的属性,这里选择全部都进行转换
                }),
                autoprefixer({ // 自动添加前缀
                    overrideBrowserslist: [
                        "Android 4.1",
                        "iOS 7.1",
                        "Chrome > 31",
                        "ff > 31",
                        "ie >= 8"
                        //'last 2 versions', // 所有主流浏览器最近2个版本
                    ],
                    grid: true
                })
            ]
        }
    }
})

4. 在根目录src下建立文件夹utils,然后创建rem.js文件 并在main.js中引入

  • PC端 rem.js
// 自适应
const baseSize = 16;
function resize() {
    // 当前页面屏幕分辨率相对于1920宽的缩放比例,可根据自己需要修改
    let scale = document.documentElement.clientWidth / 1920;
    // 下面这一行代码可以忽略,这是我另外加的,我加这行代码是为了屏幕宽度小于1280时就不继续等比缩放了
    if (document.documentElement.clientWidth < 1280) scale = 1280 / 1920
    // 设置页面根节点字体大小(“Math.min(scale, 2)” 指最高放大比例为2,可根据实际业务需求调整)
    document.documentElement.style.fontSize = `${baseSize * Math.min(scale, 1)}px`;
}
resize();
window.onresize = resize;
  • 移动端rem.js
// 自适应
    function resize() {
        let fs = document.body.clientWidth / 75; 
        // 上面的75是根据设计图尺寸修改,例如设计图宽为1220,给左右两边各留10px,即1220-20=1200,1200/16(字体大小)等于75
  
        if (fs > 16) { // 控制字体大小,以免过大过小
            fs = 16;
        } else if (fs < 14) {
            fs = 14;
        }
        // 👇注意这里不能直接document.body.style
        document.body.parentNode.style = "font-size: " + fs + "px;";
    }
    resize();
    window.onresize = resize;
  • main.js 引入
import './utils/rem.js'
  • 也可直接写在App.vue里
<template>
    <div id="app">
    </div>
</template> 
  
<script setup>
    // 自适应
    function resize() {
        let fs = document.body.clientWidth / 75; 
        // 上面的75是根据设计图尺寸修改,例如设计图宽为1220,给左右两边各留10px,即1220-20=1200,1200/16(字体大小)等于75
  
        if (fs > 16) { // 控制字体大小,以免过大过小
            fs = 16;
        } else if (fs < 14) {
            fs = 14;
        }
        // 👇注意这里不能直接document.body.style
        document.body.parentNode.style = "font-size: " + fs + "px;";
    }
    resize();
    window.onresize = resize;
</script>
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于使用 Vite + Vue3 + TypeScript + Pinia + Vue Router + Axios + SCSS 并自动导入 API 的设置,你可以按照以下步骤进行操作: 1. 首先,确保你已经安装了 Node.js,并且版本大于等于 12.0.0。 2. 创建一个新的 Vue 项目,可以使用 Vue CLI 或者手动创建一个空文件夹。 3. 在项目根目录下,打开终端并执行以下命令安装 Vite: ```bash npm init vite@latest ``` 按照提示选择你的项目配置,包括选择 Vue 3、TypeScript 和其他选项。 4. 进入项目目录并安装依赖: ```bash cd your-project-name npm install ``` 5. 安装 Pinia 插件: ```bash npm install pinia ``` 6. 创建一个 `src/store` 目录,并在其中创建 `index.ts` 文件,用于定义和导出你的 Pinia store。 ```typescript // src/store/index.ts import { createPinia } from 'pinia' export const store = createPinia() // 可以在这里定义你的 store 模块 ``` 7. 在项目根目录下创建 `src/api` 目录,用于存放 API 请求相关的文件。 8. 在 `src/api` 目录下创建一个 `index.ts` 文件,用于自动导入所有 API 文件。 ```typescript // src/api/index.ts const modules = import.meta.globEager('./*.ts') const apis: any = {} for (const path in modules) { if (path !== './index.ts') { const moduleName = path.replace(/^.\/|\.ts$/g, '') apis[moduleName] = modules[path].default } } export default apis ``` 这样,你就可以在 `src/api` 目录下创建各种 API 请求的文件,例如 `user.ts`: ```typescript // src/api/user.ts import axios from 'axios' export function getUser(id: number) { return axios.get(`/api/user/${id}`) } ``` 然后,在你的组件中使用自动导入的 API: ```typescript import { defineComponent, ref } from 'vue' import { useUserStore } from '@/store' import apis from '@/api' export default defineComponent({ setup() { const userStore = useUserStore() const userId = ref(1) const fetchUser = async () => { const response = await apis.user.getUser(userId.value) userStore.setUser(response.data) } return { userId, fetchUser, } }, }) ``` 以上就是使用 Vite + Vue3 + TypeScript + Pinia + Vue Router + Axios + SCSS 并自动导入 API 的基本设置。你可以根据自己的需求进一步配置和扩展。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值