vue3 + ts +element-plus + vue-router + scss + axios搭建项目

news/2024/7/11 0:40:48 标签: vue.js, 前端, typescript, sass, 前端框架, es6

本地环境:

node版本:20.10.0

目录

一、搭建环境

二、创建项目

三、修改页面

四、封装路由vue-router

五、element-plus

六、安装scss

七、封装axios


一、搭建环境

1、安装vue脚手架

npm i -g @vue/cli

2、查看脚手架版本

vue -V

3、切换路径到需要创建项目的地方

二、创建项目

1、创建项目

npm create vite@latest

2、根据提示依次输入以下命令:(安装依赖并运行项目)

1、cd vue3_test  //切换到项目根目录下
2、npm i         //安装依赖
3、npm run dev    //启动项目

3、浏览器打开

三、修改页面

1、删除原本的helloword页面,在src下新建view/home/index.vue文件

2、修改App.vue文件

在 Vue 3 中,<RouterView> 组件是 Vue Router 4 的一部分,用于渲染匹配到的路由组件。

<script setup lang="ts">
</script>

<template>
  <RouterView></RouterView>
</template>

<style scoped>

</style>

四、封装路由vue-router

vue3需要安装4.0以上版本

vue2需要安装4.0以下版本

1、安装路由

npm i vue-router

2、在src下新建router/index.ts文件

  router/index.ts

// src/router/index.ts
import { createRouter, createWebHashHistory } from "vue-router";
const routes: any = [
  {
    path: "/",
    redirect: "/index",
  },
  {
    path: "/index",
    component: () => import("../view/home/index.vue"),
    name: "Index",
    menuShow: true,
  },
];
const router = createRouter({
  history: createWebHashHistory(),
  routes: routes,
});

export default router;

3、在main.ts中引入

  main.ts

import { createApp } from 'vue';
import router from './router/index';
import App from './App.vue';

const app = createApp(App);
app.use(router);
app.mount('#app');

五、element-plus

1、安装

npm install element-plus

2、全局引用

//main.ts
import { createApp } from 'vue';
import './style.css'
import App from './App.vue';
import router from './router';
import ElementUI from 'element-plus'; // 引入Element Plus配置
import "element-plus/dist/index.css"

const app = createApp(App);
app.use(router);
app.use(ElementUI);
app.mount('#app');

六、安装scss

npm install sass

七、封装axios

1、安装axios

npm i axios

2、src下新建utils/axios.ts文件

// axios.ts
import axios from "axios";
import { ElMessage } from 'element-plus'
const service = axios.create({
    baseURL: '',  //访问后端接口,例如:192.168.1.131:8090/
    timeout: 3000,
})
// 请求拦截器
service.interceptors.request.use(
    (config) => {
        // 在发送请求之前做些什么,例如添加请求头等
        return config;
    },
    (error) => {
        // 对请求错误做些什么
        return Promise.reject(error);
    }
);
// 相应拦截器
service.interceptors.response.use(
    (response) => {
        // 对响应数据做些什么,例如处理错误信息等
        return response;
    },
    (error) => {
        if (error && error.response) {
            switch (error.response.status) {
                case 400:
                    error.message = '400:请求错误'
                    break
                case 403:
                    error.message = '403:拒绝访问'
                    break
                case 404:
                    error.message = `404:请求地址出错: ${error.response.config.url}`
                    break
                case 408:
                    error.message = '408:请求超时'
                    break
                case 500:
                    error.message = '500:服务器内部错误,请联系管理员'
                    break
                case 501:
                    error.message = '501:服务未实现'
                    break
                case 502:
                    error.message = '502:网关错误'
                    break
                case 503:
                    error.message = '503:服务不可用'
                    break
                case 504:
                    error.message = '504:网关超时'
                    break
                case 505:
                    error.message = '505:HTTP版本不受支持'
                    break
                default:
            }
        }
        ElMessage({
            message: error.message,
            type: 'error',
            duration: 3 * 1000,
        })
        console.error(error);

        // 对响应错误做些什么
        return Promise.reject(error);
    }
);

export default service

3、src下新建api文件下,api/home_api.ts

import request from "../utils/axios"
export function getBannerApi(data: any) {
    return request({
        url: "接口地址",
        method: "请求方式",  //(post或者get)
        params:data     //(如果是post请求,直接是data)
    })
}

4、页面引用接口,并请求

<script lang="ts" setup>
import { ref, onMounted, reactive } from "vue"
import * as homeInfoApi from '../../api/home_api';
let newsList = reactive([])
const total = ref(0 as any);
const pageSize = ref(20 as any);
const pageIndex = ref(1 as any);
onMounted(() => {
    getProductData()
})
const getProductData = async () => {
    try {
        // 参数
        let queryParams = {
            limit: pageSize.value,
            offset: pageIndex.value,
            sid: 415,
        }
        // 请求接口
        const response = await homeInfoApi.getBannerApi(queryParams);
        response.data.data.forEach((element: any) => {
            newsList.push(element)
        });
        total.value = response.data.pagination.total;
        console.log(response)
    } catch (error) {
        console.error(error);
    } finally {
    }
}

</script>

八、配置vite.config.ts

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';

export default ({ command, mode }: { command: string; mode: string }) => {
  const config = defineConfig({
    plugins: [vue()],
    server: {
      port: 8090, // 设置新的端口(前端接口)
      open: true, // 自动打开浏览器
      host: "192.168.1.130",//本地主机地址
      proxy: {
        '/api': {
          target: 'http://192.168.1.131:8081/',   //请求后端接口
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ''),
        },
      }
    },
  });
  return config;
};


http://www.niftyadmin.cn/n/5443593.html

相关文章

科大讯飞开放平台-python语音转文字教程

文章目录 简介实际使用代码coding简介 科大讯飞的语音转写(Long Form ASR)——基于深度全序列卷积神经网络,将长段音频(5小时以内)数据转换成文本数据,为信息处理和数据挖掘提供基础。 转写的是已录制音频(非实时),音频文件上传成功后进入等待队列,待转写成功后用户…

【MySQL】对表的相关操作(DDL)

&#x1f466;个人主页&#xff1a;Weraphael ✍&#x1f3fb;作者简介&#xff1a;目前学习计网、mysql和算法 ✈️专栏&#xff1a;MySQL学习 &#x1f40b; 希望大家多多支持&#xff0c;咱一起进步&#xff01;&#x1f601; 如果文章对你有帮助的话 欢迎 评论&#x1f4ac…

go|sync系列:WaitGroup、Once、Cond

文章目录 sync.WaitGroup使用方式底层原理AddDoneWait总结 sync.Once存在的意义使用方式第一个例子&#xff0c;开启十个协程利用once运行同一个函数第二个例子&#xff0c;懒汉单例获取配置文件 底层原理存在的问题改进sync.Once解决问题 sync.Cond使用方式底层原理 参考文章 …

UE5制作一条底部挂着物体的悬垂的绳子

主要涉及cable&#xff08;缆索&#xff09;组件、PhysicsConstraint&#xff08;物理约束&#xff09;组件的灵活运用&#xff0c;经过摸索&#xff0c;写下本文以供探讨。 一、关卡中制作 关卡中制作最简单 1. cable组件加入场景 打开放置Actor面板&#xff0c;在其中找到…

设计模式(行为型设计模式——备忘录模式)

设计模式&#xff08;行为型设计模式——备忘录模式&#xff09; 备忘录模式 基本定义 在不破坏封装的前提下&#xff0c;捕获一个对象的内部状态&#xff0c;并在该对象之外保存这个状态&#xff0c;这样可以在以后将对象恢复到原先保存的状态。 模式结构 Originator&#x…

爬虫工作量由小到大的思维转变---<第四十九章 Scrapy 降维挖掘---中间件系列(1)>

前言&#xff1a; Scrapy是一个功能强大的网络爬虫框架&#xff0c;但在实际应用过程中&#xff0c;中间件问题可能会成为一个令人头痛的难题。为了彻底解决Scrapy中的各种疑难杂症&#xff0c;我决定进行第四次全面的学习和实践&#xff0c;并将中间件的问题一一拆解&#xff…

【tensorflow_gpu】安装合集

tensorflow_gpu与CUDA、cuDNN、Python版本对应关系 版本对应列表 tensorflow的清华源wheel tensorflow的清华源wheel列表 tensorflow_gpu安装指令 使用conda安装指定版本的tensorflow_gpu conda install tensorflow-gpu1.2.0使用wheel安装指定版本的tensorflow_gpu pip …

网关层针对各微服务动态修改Ribbon路由策略

目录 一、介绍 二、常规的微服务设置路由算法方式 三、通过不懈努力&#xff0c;找到解决思路 四、验证 五、总结 一、介绍 最近&#xff0c;遇到这么一个需求&#xff1a; 1、需要在网关层&#xff08;目前使用zuul&#xff09;为某一个服务指定自定义算法IP Hash路由策…