当前位置:   article > 正文

搭建vue3+vite+vant移动端项目_vite 同时引入 router 和vant

vite 同时引入 router 和vant

如何使用前端技术搭建一个移动端项目。我们将使用Vue3作为前端框架,Vite作为构建工具,以及Vant作为移动端UI组件库。

初始化项目

兼容性:Vite 需要 Node.js 版本 >= 12.0.0。

vite提供不同工具的初始化

1、通过npm初始化

npm init vite@latest

2、通过Yarn

yarn create vite

3、通过PNPM

pnpm dlx create-vite

以npm为例: 

1、项目名称 

2、选择框架 

3、是否需要引入ts 

4、完成 

通过指定具体的模板 

通过附加的命令行选项直接指定项目名称和你想要使用的模板。例如要构建一个 Vite + Vue 项目,可运行一下命令: 

  1. # npm 6.x
  2. npm init vite@latest my-vue-app --template vue
  3. # npm 7+, 需要额外的双横线:
  4. npm init vite@latest my-vue-app -- --template vue
  5. # yarn
  6. yarn create vite my-vue-app --template vue

引入vue-router 

 安装vue-router

npm install vue-router -S

新建router目录,新建index.js文件 

  1. // index.js
  2. import { createRouter, createWebHashHistory } from 'vue-router'
  3. import App from '@/App.vue'
  4. // 路由规则
  5. const routes = [
  6. {
  7. path: '/',
  8. component: App,
  9. children: [
  10. { path: '', redirect: '/home' },
  11. {
  12. path: '/home',
  13. name: 'home',
  14. component: () => import('@/views/Home.vue')
  15. },
  16. {
  17. path: '/login',
  18. name: 'login',
  19. component: () => import('@/views/Login.vue')
  20. },
  21. {
  22. // vue-router@4的变化,舍弃*通配符
  23. // 官方文档:https://next.router.vuejs.org/zh/guide/migration/index.html#%E5%88%A0%E9%99%A4%E4%BA%86-%EF%BC%88%E6%98%9F%E6%A0%87%E6%88%96%E9%80%9A%E9%85%8D%E7%AC%A6%EF%BC%89%E8%B7%AF%E7%94%B1
  24. path: '/:pathMatch(.*)*',
  25. name: '404',
  26. component: () => import('@/views/404.vue')
  27. }
  28. ]
  29. }
  30. ]
  31. const router = createRouter({
  32. // vueRouter@3版本的mode改成了history,hash模式配置createWebHashHistory,history模式配置createWebHistory
  33. history: createWebHashHistory(),
  34. routes
  35. })
  36. export default router

main.js中引入router 

  1. import router from '@/router'
  2. const app = createApp(App)
  3. // 路由
  4. app.use(router)

引入axios

安装axios

npm install axios -S

utils/http目录新建index.js 

  1. import axios from 'axios'
  2. const service = axios.create({
  3. // baseURL: '',
  4. timeout: 30 * 1000,
  5. // 请求是否携带cookie
  6. withCredentials: true
  7. })
  8. // 请求拦截器
  9. // service.interceptors.request.use(config => {
  10. // }, err => {
  11. // })
  12. // 响应拦截器
  13. // service.interceptors.response.use(res => {
  14. // }, err => {
  15. // })
  16. export default service

安装vant 

npm i vant@next -S

引入vant rem适配方案 

具体可查看官方文档中的rem适配方案

安装amfe-flexible动态计算html的font-szie

npm i amfe-flexible -S

在main.js中引入amfe-flexible 

import 'amfe-flexible'

安装postcss-pxtorem 

npm i postcss-pxtorem -D

vite.config.js中加入以下配置 

  1. // Vite自身已经集成PostCSS,无需再次安装。另外也无需单独创建PostCSS配置文件,已集成到vite.config.js的css选项中
  2. import postCssPxToRem from 'postcss-pxtorem'
  3. css: {
  4. postcss: {
  5. plugins: [
  6. postCssPxToRem({
  7. rootValue: 37.5,
  8. propList: ['*'],
  9. })
  10. ]
  11. }
  12. },

引入less

安装less和less-laoder

npm i less less-loader -D

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/970877
推荐阅读
相关标签
  

闽ICP备14008679号