当前位置:   article > 正文

使用Vite构建项目 安装axios,element-plus,vue-router等配置及_vite axios

vite axios

1.安装Vite 

npm init vite@latest

2.创建项目

输入项目名称

3.安装router

  1. npm install vue-router --save
  2. import { createRouter,createWebHashHistory} from 'vue-router';
  3. import Login from '../views/login/login.vue';
  4. const routes=[
  5. {
  6. path:'/',
  7. redirect:"/Login"
  8. },
  9. {
  10. path: '/Login',
  11. name: 'Login',
  12. component: Login
  13. }
  14. ]
  15. const router = createRouter({
  16. routes,
  17. history: createWebHashHistory(),
  18. // history: createWebHistory()
  19. })
  20. router.beforeEach((to, from, next) => {
  21. if (to.path === "/login" || to.path === "/") {
  22. next();
  23. } else {
  24. let login_sucess = localStorage.getItem('login_sucess');
  25. if (login_sucess === null || login_sucess === '') {
  26. ElMessageBox.alert("请重新登录", "title", {
  27. confirmButtonText: "OK",
  28. callback: (action: any) => {
  29. next('/login');
  30. },
  31. })
  32. } else {
  33. next();
  34. }
  35. }
  36. });
  37. export default router;

4.安装axios

  1. npm install axios -g
  2. npm install qs
  3. 然后在 vite-env.d.ts 文件中导入
  4. declare module 'qs'
  5. 封装
  6. import axios from 'axios';
  7. import qs from 'qs';
  8. import { ElMessageBox } from "element-plus";
  9. import router from "../router/index"
  10. const BASEURL ='url';
  11. const service = axios.create({
  12. timeout: 5000,
  13. baseURL: BASEURL,
  14. })
  15. // http request 拦截器
  16. service.interceptors.request.use(config => {
  17. // 0. 过滤获取token的请求
  18. if (config.url === '/api/login/get_verification') {
  19. return config
  20. }
  21. // 获取 token 和 sk
  22. let token = localStorage.getItem('token') || '';
  23. let sk = localStorage.getItem('sk') || '';
  24. // 1. 获取请求参数 row
  25. // let data = config.data;
  26. // let row = data.row
  27. let data = config?.data || (config['data'] = []);
  28. let row = data?.row || (config['data']['row'] = []);
  29. config.data = qs.stringify(data)
  30. return config
  31. }, err => {
  32. return err
  33. })
  34. service.interceptors.response.use(response => {
  35. //接收返回数据..
  36. const res = response.data
  37. const { code, msg } = response.data
  38. if (code === 0) {
  39. showMessage(res, 'success')
  40. return res
  41. }
  42. else if (code === -3) {
  43. showMessage(res, 'error')
  44. return res
  45. // 抛出异常
  46. }
  47. else if (code === -9998) { // token认证失败
  48. showMessageLOGIN(res)
  49. return Promise.reject(msg) // 抛出异常
  50. } else {
  51. showMessage(res, 'error')
  52. return Promise.reject(msg || '服务器异常')
  53. }
  54. }, err => {
  55. return err;
  56. })
  57. //封装错误提示信息..
  58. function showMessage(res: any, types: string) {
  59. if (types == 'error') {
  60. ElMessage({
  61. message: res.msg,
  62. type: 'error',
  63. })
  64. } else {
  65. ElMessage({
  66. message: res.msg,
  67. type: 'success',
  68. })
  69. }
  70. // ElMessageBox.alert(res.msg, "舜泰汽车", {
  71. // confirmButtonText: "OK",
  72. // })
  73. }
  74. function showMessageLOGIN(res: any) {
  75. ElMessageBox.alert(res.msg, "舜泰汽车", {
  76. confirmButtonText: "OK",
  77. callback: (action: any) => {
  78. setTimeout(() => {
  79. router.push('/login')
  80. }, 15000)
  81. },
  82. })
  83. }
  84. export default service;
  85. 调用
  86. import service from "../../request/index"
  87. export function get_data(vin) {
  88. return service({
  89. url: "/api/api/get_data",
  90. method: 'POST',
  91. data: {
  92. row: {
  93. vin:vin,
  94. }
  95. }
  96. })
  97. }

5.安装element-plus

npm install element-plus --save

6.安装pinina

  1. npm i pinia
  2. import { defineStore } from 'pinia' //引入pinia
  3. //这里官网是单独导出 是可以写成默认导出的 官方的解释为大家一起约定仓库用use打头的单词 固定统一小仓库的名字不易混乱
  4. export const useCounter = defineStore('counter',{
  5. state: () => {
  6. return {
  7. person: {
  8. name: 'pinia',
  9. age: 0,
  10. say: 'woc'
  11. },
  12. reason: '我爱前端',
  13. }
  14. },
  15. getters:{
  16. doubleCounter(state){
  17. return state.reason="aaa"
  18. }
  19. },
  20. actions:{
  21. increment(){
  22. this.person.age++
  23. },
  24. //传参
  25. incrementnum(num){
  26. this.person.age += num
  27. }
  28. }
  29. })
  30. 调用
  31. import { useCounter } from '@/store';
  32. const store = useCounter ();

7.main.ts文件

  1. import { createApp } from 'vue'
  2. import './style.css'
  3. import App from './App.vue'
  4. import router from './router'
  5. import ElementPlus from 'element-plus'
  6. import 'element-plus/dist/index.css'
  7. import locale from "element-plus/lib/locale/lang/zh-cn"
  8. import { createPinia } from 'pinia'
  9. createApp(App).use(router).use(ElementPlus,{locale}).use(createPinia()).mount('#app')

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/227629
推荐阅读
相关标签
  

闽ICP备14008679号