vue3创建手机端应用

393 阅读1分钟

此项目是vue 手机端

  1. npm init vue 专门创建vue的项目
  2. npm create vite这个方法也可以创建vue项目
  3. npm i postcss-px-to-viewport -D专门用来做手机端像素适配的插件
  4. 如果是ts的话postcss-px-to-viewport暂时没有相对应的声明文件,需要自己写一个
  5. npm i sass -D
  6. 搭建了一个架子git地址: github.com/DDXY1230/lx…
  • 在根目录下新建 postcss-px-to-viewport.d.ts
declare module 'postcss-px-to-viewport' {
 
    type Options = {
        unitToConvert: 'px' | 'rem' | 'cm' | 'em',
        viewportWidth: number,
        viewportHeight: number, // not now used; TODO: need for different units and math for different properties
        unitPrecision: number,
        viewportUnit: string,
        fontViewportUnit: string,  // vmin is more suitable.
        selectorBlackList: string[],
        propList: string[],
        minPixelValue: number,
        mediaQuery: boolean,
        replace: boolean,
        landscape: boolean,
        landscapeUnit: string,
        landscapeWidth: number
    }
 
    export default function(options: Partial<Options>):any
}
  • tsconfig.node.json中include添加"postcss-px-to-viewport.*"
{
  "extends": "@vue/tsconfig/tsconfig.node.json",
  "include": ["vite.config.*", "vitest.config.*", "cypress.config.*", "playwright.config.*", "postcss-px-to-viewport.*"],
  "compilerOptions": {
    "composite": true,
    "types": ["node"]
  }
}
  • vite.config.ts配置
import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import pxtoViewport from 'postcss-px-to-viewport'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue(), vueJsx(), ],
  css: {
    postcss: {
      plugins: [
        pxtoViewport({
          unitToConvert: 'px', // 要转化的单位
          viewportWidth: 320, // UI设计稿的宽度
          // unitPrecision: 6, // 转换后的精度,即小数点位数
          // propList: ['*'], // 指定转换的css属性的单位,*代表全部css属性的单位都进行转换
          // viewportUnit: 'vw', // 指定需要转换成的视窗单位,默认vw
          // fontViewportUnit: 'vw', // 指定字体需要转换成的视窗单位,默认vw
          // selectorBlackList: ['ignore-'], // 指定不转换为视窗单位的类名,
          // minPixelValue: 1, // 默认值1,小于或等于1px则不进行转换
          // mediaQuery: true, // 是否在媒体查询的css代码中也进行转换,默认false
          // replace: true, // 是否转换后直接更换属性值
          // landscape: false // 是否处理横屏情况
        })
      ]
    }
  },
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  }
})