vue3.0 + ts 项目实现pc端和移动端的适配+判断设备

vue3.0 + ts 项目实现pc端和移动端的适配

写在前面:最近写前端项目需要做移动设配和PC端的适配,移动端适配包括了平板和各式各样的手机。之前都是直接使用自己做的rem换算单位来进行适配,但是,现在发现没法适配平台。最近一直在查资料,可算是让我找着了一款适合的了。
"postcss-pxtorem": "^5.1.1"

就是这个插件,它可以自动将px转化成rem, 也就意味着我们可以不再使用之前自己写的转化了,在css中直接写px就可以了。

1、首先我们来看一下需要配置的地方

1)先写一个rem.js 或者是 rem.ts放在配置文件夹里。并且在main.js 或者 main.ts中进行引入。

// 基准大小
const baseSize = 32;
// 设置 rem 函数
function setRem() {
  // 当前页面宽度相对于 750 宽的缩放比例,可根据自己需要修改。
  const scale = document.documentElement.clientWidth / 750;
  // 设置页面根节点字体大小
  document.documentElement.style.fontSize =
    baseSize * Math.min(scale, 2) + "px";
}
// 初始化
setRem();
// 改变窗口大小时重新设置 rem
window.onresize = function () {
  setRem();
};

2)在项目的根目录下写一个这个文件:postcss.config.js
在这里插入图片描述
这个文件中是写html根节点大小的,内容如下:

module.exports = {
  plugins: {
    autoprefixer: {},
    "postcss-pxtorem": {
      rootValue: 16,
      propList: ["*"],
      exclude: "pc", // 排除pc
    },
  },
};

2、注意点

因为我的项目是需要写pc端和移动端的。所以说一下我是怎么做的。
移动端的设配,如果 ui 给的设计稿是 375宽的,这里就可以直接使用px写了,也就是设计稿上面的文字是多少px,你就直接写多少px,插件会帮你自动转化的。
pc端的可以上面写的配置里面有排除pc端不一定生效。而且一般来说我们的pc端都是很少做适配的,你可以给一个最小的宽度,当用户的电脑屏幕比最小的宽度还要小的时候,就会产生滚动条 ,这样就不存在布局乱了的问题。注意,写pc端的时候一定要忽略postcss-pxtorem插件的影响。pc也不用太麻烦,就直接将px这个单位里面的字母大写就可以忽略了,比如PX,Px都可以。重点:使用的vscode 或者webstorm的时候要取消eslint的格式自动 转化,如果没有关闭,会导致你的项目里面的PX或者Px全部自动转换成了小写的px。

判断不同的设备,pc 和 移动端各有一套html 和css 代码

核心:两套资源+判断客户端类型+路由守卫

1、router.ts 文件

pc端的统一以 p 开头 ,并加上meta: { type: “pc” },
移动端的以 m 开头 ,并加上 meta: { type: “mobile” },

import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
const redirectPath = /Android|webOS|iPhone|iPod|BlackBerry|iPad/i.test(
  navigator.userAgent
)
  ? "/m_home"
  : "/p_home";
const routes: Array<RouteRecordRaw> = [
  {
    path: "/p_home",
    name: "p_home",
    component: () => import("@/views/p_views/Home/index.vue"),
    meta: { type: "pc" },
  },
  {
    path: "/p_busd",
    name: "p_busd",
    component: () => import("@/views/p_views/BUSD/index.vue"),
    meta: { type: "pc" },
  },
  //  path /m_   type:mobile

  {
    path: "/m_home",
    name: "m_home",
    component: () => import("@/views/m_views/Home/home.vue"),
    meta: { type: "mobile" },
  },
  {
    path: "/m_busd",
    name: "m_busd",
    component: () => import("@/views/m_views/BUSD/busd.vue"),
    meta: { type: "mobile" },
  },

  {
    path: "/",
    redirect: redirectPath,
  },
];
const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});

export default router;

2、main.ts 文件

引入上面的router.ts文件
import router from “./router”;
然后使用下面的代码进行判断是pc 端还是移动端:

//路由操作
router.beforeEach((to, from, next) => {
  // 当移动端试图访问pc端界面时
  if (
    /Android|webOS|iPhone|iPod|BlackBerry|iPad/i.test(navigator.userAgent) &&
    to.meta.type !== "mobile"
  ) {
    const routers = router.options.routes.filter((v) => v.name?.toString().startsWith("m") );
    const path = routers?.filter(
      (v) =>
        v.name?.toString().split("_")[1] === to.name?.toString().split("_")[1]
    )[0];
    if (path) {
      next(path.path);
    } else {
      next("/");
    }
  }

  // 当pc端页面试图访问移动端页面时
  if (
    !/Android|webOS|iPhone|iPod|BlackBerry|iPad/i.test(navigator.userAgent) &&
    to.meta.type !== "pc"
  ) {
    const routers = router.options.routes;
    const path = routers.filter(
      (v) =>
        v.name?.toString().split("_")[1] === to.name?.toString().split("_")[1]
    )[0].path;
    if (path) {
      next(path);
    } else {
      next("/"
      );
    }
  }
  next();
});

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值