vue-cli,element-plus,axios,proxy

一、vue-cli

vue-cli俗称vue脚手架,是vue官方提供的快速生成vue 工程化项目的工具。

1.官网:https://cn.vuejs.org/ 

中文官网: https://cli.vuejs.org/zh/

特点:基于webpack,功能丰富且易于扩展,支持创建vue2和vue3的项目

2.全局安装:
npm install -g @vue/cli

 查看vue-cli的版本,检查vue-cli是否安装成功

vue --version

 

3.解决Windows PowerShell 不识别vue命令的问题

a.以管理员身份运行 PowerShell

b.执行set-ExecutionPolicy RemoteSigned命令

c.输入字符Y,回车即可

4.基于vue ui 创建vue项目

本质:通过可视化面板采集到的用户配置信息后,在后台基于命令行的方式自动初始化项目

a.在终端下运行vue ui 命令,自动在浏览器中打开创建项目的可视化面板 

b.在详情页面填写vue项目名称

c. 在预设页面选择手动配置项目

d.在功能页面勾选需要安装的功能(css预处理器,使用配置文件)

e.在配置页面勾选vue的版本和需要的预处理器

f.将刚才所有的配置保存为预设模板,方便下一次创建项目时直接复用之前的配置 

5.基于命令行创建vue项目
vue create my-project

a.在终端下运行vue create 002demo命令,基于交互式的命令行创建vue的项目

b.选择要安装的功能(手动选择要安装的功能)

把babel,eslint等插件的配置信息存储到单独的配置文件中(推荐)

把babel,eslint等插件的配置信息存储到package.json中(不推荐)

erer

二、组件库

1.element-plus

地址:https://element-plus.org/zh-CN/

全局引入

npm install element-plus --save

npm install @element-plus/icons-vue

// main.js
import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import App from './App.vue'
const app = createApp(App)
app.use(ElementPlus)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}
app.mount('#app')

也可以将element相关代码拆分

element.js
import { ElButton,ElIcon } from 'element-plus'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
export const setupElement = (app) =>{
    app.component(ElButton.name, ElButton)
    app.component(ElIcon.name, ElIcon)
    for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
        app.component(key, component)
      }
}

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import {setupElement} from './element.js'
import 'element-plus/dist/index.css'
const app = createApp(App)
app.use(router)
setupElement(app)
app.mount('#app')
按需引入

 npm install -D unplugin-vue-components unplugin-auto-import

vue.config.js
const AutoImport = require('unplugin-auto-import/webpack').default;
const Components = require('unplugin-vue-components/webpack').default;
const { ElementPlusResolver } = require('unplugin-vue-components/resolvers');
module.exports = {
  configureWebpack: {
    resolve: {
      alias: {
        components: '@/components'
      }
    },
    //配置webpack自动按需引入element-plus,
    plugins: [
      AutoImport({
        resolvers: [ElementPlusResolver()]
      }),
      Components({
        resolvers: [ElementPlusResolver()]
      })
    ]
  }
};
<template>
  <div class="hello">
    <el-button color="#626aef">Default</el-button>
    <el-button>我是 ElButton</el-button>
    <el-button type="primary" circle>
      <el-icon :size="20">
        <Edit />
      </el-icon>
    </el-button>
  </div>
</template>
<script>
import { Edit } from '@element-plus/icons-vue'
export default {
  name: 'HelloWorld',
  components: {
    Edit
  }
}
</script>
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
// import {setupElement} from './element.js'
import 'element-plus/dist/index.css'
const app = createApp(App)
app.use(router)
// setupElement(app)
app.mount('#app')

三、axios拦截器

拦截器会在每次发起ajax请求和得到相应的时候自动被触发。

应用场景:token身份验证,loading效果。

main.js
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import axios from 'axios'
import 'element-plus/dist/index.css'
const app = createApp(App)
app.use(router)
axios.defaults.baseURL='http://localhost:3000'
app.config.globalProperties.$http=axios
app.mount('#app')

<template>
</template>
<script>
export default {
  methods:{
    async getData(){
      const {data:res} = await this.$http.get('/goodsList')
      console.log('res',res);
    }

  },
  created(){
    this.getData()
  }
}
</script>
配置请求拦截器,响应拦截器

通过axios.interceptors.request.use(成功的回调,失败的回调)可以配置请求拦截器。

通过axios.interceptors.response.use(成功的回调,失败的回调)可以配置相应拦截器。

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import axios from 'axios'
import 'element-plus/dist/index.css'
import { ElLoading  } from 'element-plus'
const app = createApp(App)
app.use(router)
axios.defaults.baseURL='http://localhost:3000'
// axios.interceptors.request.use(config=>{
//     config.headers.Authorization='Bearer xxx'
//     return config
// })
let loadingInstance=null
axios.interceptors.request.use(config=>{
    loadingInstance = ElLoading.service({fullscreen:true})
    return config
})
axios.interceptors.response.use((response)=>{
    loadingInstance.close()
    return response
},(error)=>{return Promise.reject(error)} )
app.config.globalProperties.$http=axios
app.mount('#app')

拆分axios

// src/http.js
import axios from 'axios';
import { ElLoading } from 'element-plus';

const http = axios.create({
    baseURL: 'http://localhost:3000',
});

let loadingInstance = null;

http.interceptors.request.use(config => {
    loadingInstance = ElLoading.service({ fullscreen: true });
    if(localStorage.getItem('token')){
        config.hearders.token=localStorage.getItem('token')
    }
    return config;
});

http.interceptors.response.use(
    response => {
        loadingInstance.close();
        return response;
    },
    error => {
        loadingInstance.close();
        switch(error.response.status){
            case 404:console.log("您请求的路径不存在,或者错误");break;
            case 500:console.log("服务器出错");break;
        }
        return Promise.reject(error);
    }
);

export default http;

main.js
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import http from './http'
import 'element-plus/dist/index.css'
const app = createApp(App)
app.use(router)
app.config.globalProperties.$http=http
app.mount('#app')

拆分发请求的api

api.js
import http from "./http";
//获取商品列表
// export const getGoodsListApi=()=>{
//     return http.get("/goodsList")
// }
export const getGoodsListApi=()=>{
    return http({
        url:"/goodsList",
        methods:'get'
    })
}

四、proxy跨域代理

1.解决方法

a.把axios的请求根路径设置为vue项目的根路径

b.vue项目发请请求的接口不存在,把请求转交给proxy代理

c.代理把请求路径替换为devServer.proxy属性的值,发请真正的数据请求

d.代理把请求的数据,转发为axios

vue.config.js
const AutoImport = require('unplugin-auto-import/webpack').default;
const Components = require('unplugin-vue-components/webpack').default;
const { ElementPlusResolver } = require('unplugin-vue-components/resolvers');
const { defineConfig } = require('@vue/cli-service');

module.exports = defineConfig({
  configureWebpack: {
    resolve: {
      alias: {
        components: '@/components'
      }
    },
    plugins: [
      AutoImport({
        resolvers: [ElementPlusResolver()]
      }),
      Components({
        resolvers: [ElementPlusResolver()]
      })
    ]
  },
  devServer: {
    proxy: {
      '/apicity': { //axios访问 /apicity ==  target + /apicity
        target: 'http://121.89.205.189:3000',//真正的服务器
        changeOrigin: true, //创建虚拟服务器 
         pathRewrite: {
            '^/apicity': '' //重写接口地址,去掉/apicity, 
        }
      }
    }
  }
});

api.js
import http from "./http";
export const getCitysListApi=()=>{
    return http({
        url: "/apicity/city/sortCity.json",
        methods:'get'
    })
}

注意:a.derServer.proxy提供的代理功能,仅在开发调试阶段生效

b.项目上线发布时,依旧需要api接口服务器开启cors跨域资源共享

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/884828.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

对话总结:Scale AI的创始人兼CEO Alex Wang

AI的三大支柱 计算:主要由大公司如NVIDIA推动。算法:顶尖实验室如OpenAI主导。数据:Scale致力于推动数据进展。前沿数据的重要性 与人类智能相比较,前沿数据是AI发展的关键。互联网数据是机器与人类合作的结果。语言模型的发展 第一阶段:原始的Transformer论文和GPT的小规…

一书直接讲透自然语言处理《Getting Started with Google BERT_ Build and train》

《Getting Started with Google BERT: Build and Train》是一本面向初学者和中级读者的指南&#xff0c;旨在帮助他们理解和使用Google的BERT&#xff08;Bidirectional Encoder Representations from Transformers&#xff09;模型。BERT是近年来自然语言处理&#xff08;NLP&…

Linux下的git开篇第一文:git的意义

目录 1.git版本控制器 2.git gitee&&github 3.Linux中gitee的使用 &#xff08; 三板斧 git add git commit -m " " git push &#xff09; 4.git log 查看之前的修改信息 &#xff08;所有提交日志&#xff09; 5.git status 查看工作目录与本地…

透传 vs 非透传|数据传输效率与安全性的权衡及应用指南

官方原文&#xff1a;一分钟搞懂透传和非透传的区别-成都纵横指控 在当今数字化时代,数据传输已经成为各行各业的关键环节。在数据通信和物联网应用中,"透传"和"非透传"是两个常见且重要的概念。了解它们的区别,对于选择合适的通信方式至关重要。 什么是…

【java】前端RSA加密后端解密

目录 1. 说明2. 前端示例3. 后端示例3.1 pom依赖3.2 后端结构图3.3 DecryptHttpInputMessage3.4 ApiCryptoProperties3.5 TestController3.6 ApiCryptoUtil3.7 ApiDecryptParamResolver3.8 ApiDecryptRequestBodyAdvice3.9 ApiDecryptRsa3.10 ApiCryptoProperties3.11 KeyPair3…

C++(Qt)软件调试---内存调试器Dr.Memory(21)

C(Qt)软件调试—内存调试器Dr. Memory&#xff08;21&#xff09; 文章目录 C(Qt)软件调试---内存调试器Dr. Memory&#xff08;21&#xff09;[toc]1、概述&#x1f41c;2、安装Dr.Memory&#x1fab2;3、命令行使用Dr.Memory&#x1f997;4、Qt Creator集成使用Dr.Memory&…

excel快速入门(二)

Excel的概念说明 文章目录 Excel的概念说明常见术语说明单元格/单元格区域活动单元格/单元格区域行或列单元格引用相对引用绝对引用混合引用 Excel的常见格式说明单元格格式数字格式 Excel 工作表编辑鼠标指针介绍1.白色十字状2.单向黑色箭头状3.双向单竖线箭头状4.双向双竖线箭…

AI新掌舵:智享AI直播系统:直播界的新浪潮还是真人主播的终结者?

AI新掌舵&#xff1a;智享AI直播系统&#xff1a;直播界的新浪潮还是真人主播的终结者&#xff1f; 在数字化浪潮的汹涌澎湃中&#xff0c;人工智能&#xff08;AI&#xff09;以其前所未有的速度渗透至各行各业&#xff0c;其中&#xff0c;直播领域正经历着一场前所未有的变革…

C# CS1612 尝试修改集合中值类型的情况

在C#中&#xff0c;发现尝试直接修改集合中值类型的中的值发生报错 提示“它不是变量”&#xff0c;通过官方索引的链接可知&#xff0c;尝试修改某一值类型&#xff0c;但是该值类型作为中间表达式的结果生成但不存储在变量中&#xff0c;会发生报错。 正确做法是将其赋值给局…

【湖南步联科技身份证】 身份证读取与酒店收银系统源码整合———未来之窗行业应用跨平台架构

一、html5 <!DOCTYPE html> <html><head><meta http-equiv"Content-Type" content"text/html; charsetutf-8" /><script type"text/javascript" src"http://51.onelink.ynwlzc.net/o2o/tpl/Merchant/static/js…

nginx 安装(Centos)

nginx 安装-适用于 Centos 7.x [rootiZhp35weqb4z7gvuh357fbZ ~]# lsb_release -a LSB Version: :core-4.1-amd64:core-4.1-noarch Distributor ID: CentOS Description: CentOS Linux release 7.9.2009 (Core) Release: 7.9.2009 Codename: Core# 创建文件…

基于springboot vue网上摄影工作室系统设计与实现

博主介绍&#xff1a;专注于Java vue .net php phython 小程序 等诸多技术领域和毕业项目实战、企业信息化系统建设&#xff0c;从业十五余年开发设计教学工作 ☆☆☆ 精彩专栏推荐订阅☆☆☆☆☆不然下次找不到哟 我的博客空间发布了1000毕设题目 方便大家学习使用 感兴趣的…

04 面部表情识别:Pytorch实现表情识别-表情数据集训练代码

总目录&#xff1a;人脸检测与表情分类 https://blog.csdn.net/whiffeyf/category_12793480.html 目录 0 相关资料1 面部表情识数据集2 模型下载3 训练 0 相关资料 面部表情识别2&#xff1a;Pytorch实现表情识别(含表情识别数据集和训练代码)&#xff1a;https://blog.csdn.n…

Linux系统安装和配置 VNC 服务器

文章目录 1.安装 GNOME 桌面环境2.安装 VNC 服务器&#xff08;tigervnc-server&#xff09;3.为本地用户设置 VNC 密码4.设置 VNC 服务器配置文件5.启动 VNC 服务并允许防火墙中的端口 1.安装 GNOME 桌面环境 [rootserver6 ~]# dnf groupinstall "workstation" -y成…

Linux——k8s组件

kubernetes 使用1.31.1 版本搭建集群核心组件&#xff0c;选择flannel 网络插件为整体集群的运行提供网络通信功能。 flannel 网络插件 kube-flannel kube-flannel-ds-9fgml 1/1 Running 1 (18m ago) 2d21h kube-flannel kube-flannel-ds-ghwbq …

blender设置背景图怎么添加?blender云渲染选择

Blender是一款功能强大的3D建模软件&#xff0c;它以流畅的操作体验和直观的用户界面而闻名。使用Blender&#xff0c;你可以轻松地为你的3D模型添加背景图片。 以下是具体的操作步骤&#xff1a; 1、启动Blender&#xff1a;首先&#xff0c;打开Blender软件。访问添加菜单&a…

jQuery——offset 和 position

获取/设置标签的位置数据 offset&#xff08;&#xff09;&#xff1a;相对页面左上角的坐标 position&#xff08;&#xff09;&#xff1a;相对于父元素左上角的坐标 本文分享到此结束&#xff0c;欢迎大家评论区相互讨论学习&#xff0c;下一篇继续分享jQuery中scroll的学…

好用的电容笔有哪些推荐?2024盘点五款高性价比平替电容笔!

近几年&#xff0c;平板电脑等电子设备已经成为我们学习、工作和创作的重要工具。而电容笔作为这些设备的重要配件&#xff0c;更是受到了广泛地欢迎。然而&#xff0c;苹果原装电容笔价格较高&#xff0c;对于很多用户来说&#xff0c;寻找一款高性价比的平替电容笔成为了他们…

ClickHouse | 查询

1 ALL 子句 2 ARRAY JOIN 使用别名 :在使用时可以为数组指定别名&#xff0c;数组元素可以通过此别名访问&#xff0c;但数组本身则通过原始名称访问 3 DISTINCT子句 DISTINCT不支持当包含有数组的列 4 FROM子句 FROM 子句指定从以下数据源中读取数据: 1.表 2.子…

【微服务即时通讯系统】——brpc远程过程调用、百度开源的RPC框架、brpc的介绍、brpc的安装、brpc使用和功能测试

文章目录 brpc1. brpc的介绍1.1 rpc的介绍1.2 rpc的原理1.3 grpc和brpc 2. brpc的安装3. brpc使用3.1 brpc接口介绍 4. brpc使用测试4.1 brpc同步和异步调用 brpc 1. brpc的介绍 1.1 rpc的介绍 RPC&#xff08;Remote Procedure Call&#xff09;远程过程调用&#xff0c;是一…