pc 移动端 双端切换

x33g5p2x  于9个月前 转载在 其他  
字(4.2k)|赞(0)|评价(0)|浏览(370)

实现一个项目匹配多个端,使用vue.config自带的page 实现多个页面切换。官网介绍:https://cli.vuejs.org/zh/config/#pages

在创建的vue项目中找到 vue.config.js中 添加page

没有就在根目录下创建vue.config.js

  1. const { defineConfig } = require('@vue/cli-service')
  2. module.exports =defineConfig({
  3. transpileDependencies: true,
  4. //多页面配置
  5. pages: {
  6. //多个页面
  7. mobile: {
  8. //模板来源
  9. template: 'public/mobile.html',
  10. //page 的入口
  11. entry: 'src/mobile.main.ts',
  12. //在 dist/index.html 的输出
  13. filename: 'mobile.html',
  14. //页面标题
  15. title: '移动适配',
  16. },
  17. index: {
  18. template: 'public/index.html',
  19. entry: 'src/main.ts',
  20. filename: 'index.html',
  21. title: 'pc适配',
  22. }
  23. },
  24. })

vue.config.js

分别在 public, router ,views ,src下添加修改文件

public 中复制一个html 修改名字为 mobile(可以更具需求自己修改)。

router 与 view 中创建2套路由文件,分别对应pc 与 mobile

找到src 文件下的 App.vue 与 main.ts 复制一套并改名 为 mobileApp.vue 与 mobile.main.ts

router 多端路由配置

index.ts 代码

pc与mobile 下的index.ts 内容都一样,只有component 对应的页面地址 更具不同的端 ,对应不同的地址。

注:pc 与 mobile 内容,唯一要注意的是页面地址 分别对应不同的路径

  1. import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'const routes: Array<RouteRecordRaw> =[
  2. {
  3. path: "/",
  4. name: "home",
  5. //pc 或mobile 路径
  6. component: () => import('@/views/PC/HomeView/HomeView.vue'),
  7. meta: {
  8. title: "首页",
  9. //是否需要登录
  10. requireAuth: false}
  11. }
  12. ]
  13. const router =createRouter({
  14. history: createWebHashHistory(process.env.BASE_URL),
  15. //整合路由
  16. routes: routes,
  17. })
  18. /*前置 路由守卫
  19. */
  20. /*eslint-disable */router.beforeEach((to, from, next) =>{
  21. /*-----通过本地缓存进行判断,获取指定key本地存储的值进行判断----- */
  22. if(to.meta.requireAuth) {
  23. //获取指定key本地存储的值
  24. const token = localStorage.getItem('Authorization')
  25. if (token === null || token === '') {
  26. //登录弹窗
  27. console.log("去登录")
  28. } else{
  29. next()
  30. }
  31. } else{
  32. next()
  33. }
  34. })
  35. /*eslint-disable no-new */
  36. /*后置 路由守卫
  37. */router.afterEach((to: any) =>{
  38. //console.log("后置 路由守卫", to, from)
  39. //更改每个页面的标题
  40. document.title =to.meta.title;
  41. })
  42. export default router

router

Views 文件配置

在views 文件下分别创建 PC 与 Moblie 文件,对应为 pc端与移动端的页面地址。

复制一套app.vue 与main.ts ,修改复制的名字为 mobileApp.vue 与 mobile.main.ts

app.vue 与 mobileApp.vue

  1. <template>
  2. <router-view />
  3. </template>
  4. <style lang="scss">
  5. </style>

app.vue

main.ts 与 mobile.main.ts

  1. import { createApp } from 'vue'import App from './App.vue'
  2. //main 导入 pc 端路由
  3. import router from './router/pc/index'
  4. //mobile.main 导入 mobile 端路由//import router from './router/mobile/index'
  5. import store from './store'const app =createApp(App)
  6. //浏览器视口小于900px时,使用mobile路由//浏览器视口大于900px时,使用pc路由
  7. import '@/utils/convert/autoSwitch'import '@/utils/convert/rem'app.use(store).use(router).mount('#app')

main.ts

创建utils文件,在创建convert 文件

在convert文件内部创建:autoSwitch.ts ,functions.ts ,rem.ts

autoSwitch 根据页面大小, 切换显示的端口

  1. import { debounce } from './functions'window.addEventListener('resize', debounce(() =>{
  2. if (document.documentElement.clientWidth < 900) {
  3. if (window.location.href === '/mobile.html#/') returnwindow.location.href = './mobile.html#/'} else{
  4. if (window.location.href === '/index.html#/') returnwindow.location.href = './index.html#/'}
  5. }, 100))

autoSwitch.ts

rem 设置默认字体 等元素

  1. import { debounce } from './functions'
  2. functionsetRem() {
  3. //320 默认大小16px; 320px = 20rem ;每个元素px基础上/16
  4. const htmlWidth = document.documentElement.clientWidth ||document.body.clientWidth
  5. //得到html的Dom元素
  6. const htmlDom = document.getElementsByTagName('html')[0]
  7. //设置根元素字体大小
  8. const clientWidth =document.documentElement.clientWidth
  9. //1920设计稿一套样式,750设计稿一套样式
  10. htmlDom.style.fontSize = clientWidth < 900 ? htmlWidth / 46.875 + 'px' : htmlWidth / 120 + 'px'}
  11. //初始化
  12. setRem()
  13. window.addEventListener('resize', debounce(setRem, 100))

rem.ts

functions 防抖与节流函数

注:注释的为js版本使用的 ,没注释的为ts版本

  1. //防抖函数
  2. type CallbackFn = (item?: any) => voidexport functiondebounce(fn: CallbackFn, delay: number) {
  3. let timer: number | null = null;
  4. return (...args: any[]) =>{
  5. if(timer) {
  6. clearTimeout(timer);
  7. }
  8. timer = setTimeout(() =>{
  9. fn(...args);
  10. }, delay);
  11. }
  12. }
  13. //节流函数
  14. export functionthrottle(fn: CallbackFn, delay: number) {
  15. let timer: number | null = null;
  16. return (...args: any[]) =>{
  17. if(timer) {
  18. return;
  19. }
  20. timer = setTimeout(() =>{
  21. fn(...args);
  22. timer = null}, delay);
  23. }
  24. }
  25. /*js 原始版本
  26. // 防抖函数
  27. export function debounce(fn, delay) {
  28. let timer = null
  29. return function () {
  30. const _this = this
  31. const args = arguments
  32. if (timer) {
  33. clearTimeout(timer)
  34. }
  35. timer = setTimeout(function () {
  36. fn.apply(_this, args)
  37. }, delay)
  38. }
  39. }
  40. // 节流函数
  41. export function throttle(fn, delay) {
  42. let timer = null
  43. return function () {
  44. const _this = this
  45. const args = arguments
  46. if (timer) {
  47. return
  48. }
  49. timer = setTimeout(function () {
  50. fn.apply(_this, args)
  51. timer = null
  52. }, delay)
  53. }
  54. }
  55. */

functions.ts

 运行路由展示

项目地址: https://github.com/jielov/doubleEnd-switch

本文来自博客园,作者:虚乄,转载请注明原文链接:https://www.cnblogs.com/lovejielive/p/17610945.html

相关文章