• 移动端项目创建,脚手架,vant3移动开发的UI组件


     1.利用脚手架,下载文件

    npm init vue@latest

    2.新取一个名字,或者用默认的,我新取的是webmobile,根据自己的需求做相关的配置

     3.cd到自己新建的文件,再npm i 下载

    4.清理项目目录结构,不用的页面可以删除

    这里我们在views里面新建立一个根文件layout.vue,页面简单写个hello,在router里面引用,并设置路由。

    1. import Layout from "@/views/Layout.vue";
    2. const router = createRouter({
    3. history: createWebHistory(import.meta.env.BASE_URL),
    4. routes: [
    5. {
    6. path: '/',
    7. name: 'layout',
    8. component: Layout,
    9. },
    10. ]
    11. })
    12. export default router

    5.在env.d.ts声明文件中输入代码,不然会出现不兼容的一些报错

    1. ///
    2. declare module '*.vue'{
    3. import { defineComponent} from "vue"
    4. const component :defineComponent<{},{},any>
    5. }

    6.下载重置样式

    npm i normalize.css

    在我们的入口文件main.ts中引入

    import 'normalize.css'
    

    7.sass下载

    npm i sass -D

    8.Vant 3 - Lightweight Mobile UI Components built on Vue

    这个vant3是专门用于移动开发的UI组件

    •    8.1下载vant3
    npm i vant
    •    8.2按需引入组件样式
    npm i unplugin-vue-components -D
    •   8.3 配置插件,如果是基于 vite 的项目,在 vite.config.js / vite.config.ts文件中配置插件:
    1. import Components from 'unplugin-vue-components/vite';
    2. import { VantResolver } from 'unplugin-vue-components/resolvers';
    • 8.4 将vite.config.ts下暴露的函数完善:
    1. export default defineConfig({
    2. plugins: [vue(),
    3. Components({
    4. resolvers: [VantResolver()],
    5. }),],
    6. resolve:{
    7. alias: {
    8. '@': fileURLToPath(new URL('./src', import.meta.url))
    9. }
    10. }
    11. })

    9.下载网络axios

    npm i axios -S

    此时我们在src下新建立一个utils,在下面建立一个request.js,用于封装网络接口

    1. import axios from 'axios'
    2. export let baseURL = 'http://10.7.162.150:8089'
    3. /**
    4. * process.env.NODE_ENV
    5. * production 生产环境
    6. * npm run build
    7. *
    8. * development 开发环境
    9. * npm run dev
    10. *
    11. */
    12. switch (process.env.NODE_ENV) {
    13. case 'production':
    14. baseURL = 'https://api.yuguoxy.com'
    15. break
    16. case 'development':
    17. baseURL = 'http://10.7.162.150:8089'
    18. break
    19. }
    20. const axiosServer = axios.create({
    21. baseURL,
    22. timeout: 5000,
    23. })
    24. //请求拦截器
    25. axiosServer.interceptors.request.use(
    26. config => {
    27. // console.log('请求拦截器 config ', config)
    28. // 设置token到authorization头部
    29. let token = localStorage.getItem('TOKEN')
    30. if (token) {
    31. // console.log('config.headers ',config.headers);
    32. config.headers['Authorization'] = token
    33. }
    34. return config
    35. },
    36. error => {
    37. // 对请求错误做些什么
    38. return Promise.reject(error)
    39. }
    40. )
    41. //响应拦截器
    42. axiosServer.interceptors.response.use(
    43. function (response) {
    44. return response.data
    45. },
    46. function (error) {
    47. // 对响应错误做点什么
    48. return Promise.reject(error)
    49. }
    50. )
    51. export default axiosServer

    10.移动端适配

    关于移动端的适配有很多方法,这里介绍一种

    npm i postcss-px-to-viewport -D

     在 vite.config.js 中配置相关文件:

    1. import { fileURLToPath, URL } from 'node:url'
    2. import { defineConfig } from 'vite'
    3. import vue from '@vitejs/plugin-vue'
    4. import Components from 'unplugin-vue-components/vite'
    5. import { VantResolver } from 'unplugin-vue-components/resolvers'
    6. import postCssPxToRem from 'postcss-pxtorem'
    7. // https://vitejs.dev/config/
    8. export default defineConfig({
    9. plugins: [
    10. vue(),
    11. Components({
    12. resolvers: [VantResolver()],
    13. }),
    14. ],
    15. resolve: {
    16. alias: {
    17. '@': fileURLToPath(new URL('./src', import.meta.url)),
    18. },
    19. },
    20. css: {
    21. // 此代码为适配移动端px2rem
    22. postcss: {
    23. plugins: [
    24. postCssPxToRem({
    25. rootValue: 37.5, // 1rem的大小
    26. propList: ['*'], // 需要转换的属性,这里选择全部都进行转换
    27. }),
    28. ],
    29. },
    30. },
    31. })

     此时npm run dev看看是否有报错,如有报错没有 ts-node,我们安装一下即可。

    npm i ts-node -S

    此时我运行出来,跟文件layout已经显示出来了。

    现在移动端的项目框架已经创建好了,快点提交到你的远程仓库保存吧,下次直接克隆就不需要花时间配置相关文件啦~~~

     

  • 相关阅读:
    Unity3D教程:手游开发常用排序算法 -上
    【Android Jetpack】Hilt的理解与浅析
    [BSidesCF 2019]Futurella 1
    uniapp ui库 px 转 rpx
    带权并查集(poj-1182 食物链)
    hadoop生态圈面试精华之MapReduce(二)
    【ROS机器人】Autolabor Pro RTK室外厘米级导航
    【Docker】Tensorflow 容器化部署
    容器编排学习(五)卷的概述与存储卷管理
    ICMP隐蔽隧道攻击分析与检测(三)
  • 原文地址:https://blog.csdn.net/qq_63322025/article/details/127568447