当前位置: 首页 > news >正文

咸宁市做网站优化方案电子版

咸宁市做网站,优化方案电子版,网建部,做网站温州原文作者:我辈李想 版权声明:文章原创,转载时请务必加上原文超链接、作者信息和本声明。 Vue 【Vue3】env环境变量的配置和使用(区分cli和vite) 文章目录 Vue前言一、常见用法二、vue3cli封装接口1..env配置2..dev(开…

原文作者:我辈李想
版权声明:文章原创,转载时请务必加上原文超链接、作者信息和本声明。

Vue

【Vue3】env环境变量的配置和使用(区分cli和vite)


文章目录

  • Vue
  • 前言
  • 一、常见用法
  • 二、vue3+cli封装接口
    • 1..env配置
    • 2..dev(开发环境,其他环境自己配置)
    • 3.vue.config.js文件
    • 4.封装axios请求
      • 4.1 在api文件夹中新建request.js
      • 4.2 新建接口rule.js
      • 4.3 新建index.js
      • 4.4 App.vue引用
      • 4.5 全局使用api


前言


一、常见用法

我们将创建api文件夹,包含index.js和request.js,剩下的rule.js就是实际接口文件。
在这里插入图片描述

二、vue3+cli封装接口

vue3+cli的项目,配置中需要以VUE_APP开头。

1…env配置

VUE_APP_PORT = 8080

2…dev(开发环境,其他环境自己配置)

VUE_APP_API_HOST = '127.0.0.1'
VUE_APP_API_HOST = '8000'
VUE_APP_API_BASE_URL = "http://127.0.0.1:8000/api"VUE_APP_ENV = 'development'

3.vue.config.js文件

如果项目没有此文件,需要自己创建,位置参考第一部分截图。
这里的devServer配置表示代理,即将前端的host、port和路由代理至api接口。其中’/api’很重要,后边会用到。
下述代码表示将http://127.0.0.1:8080/api代理至http://127.0.0.1:8000/api)

const {defineConfig
} = require('@vue/cli-service')
module.exports = defineConfig({transpileDependencies: true,assetsDir: 'static',devServer: {// host: process.env.VUE_APP_HOST,// port: process.env.VUE_APP_PORT, //端口// https: false, //false关闭https,true为开启// open: true, //自动打开浏览器proxy: {'/api': {target: process.env.VUE_APP_API_BASE_URL,changeOrigin: true,pathRewrite: {'/api': ''}}}}
})

4.封装axios请求

4.1 在api文件夹中新建request.js

这里的baseURL: “/api"中的”/api",与第三部的代理一致。

// 导入axios
import axios from 'axios'const request = axios.create({// baseURL 将自动加在 url`前面,除非 url 是一个绝对 URL。// 它可以通过设置一个 baseURL 便于为 axios 实例的方法传递相对 URLbaseURL: "/api",// timeout设置一个请求超时时间,如果请求时间超过了timeout,请求将被中断,单位为毫秒(ms)timeout: 60000,// headers是被发送的自定义请求头,请求头内容需要根据后端要求去设置,这里我们使用本项目请求头。headers: {'Accept': 'application/json','Content-Type': 'application/json','Access-Control-Allow-Origin': '*'}
})// 请求拦截器
request.interceptors.request.use(config => {// 在请求发送之前可以做一些处理,比如添加请求头等return config;},error => {// 请求错误处理return Promise.reject(error);}
);// 响应拦截器
request.interceptors.response.use(response => {// 在这里可以对响应数据进行处理return response.data;},error => {// 响应错误处理return Promise.reject(error);}
)export default request

4.2 新建接口rule.js

import request from './request.js'export function getrules(params) {return request({url: '/rule/rule/',method: 'get',params: params})
}export function postrule(data) {return request({method: 'post',data: data,url: '/rules'})
}export function updaterule(id, data) {return request({method: 'post',data: data,url: '/rules/${id}'})
}export default {getrules,postrule,updaterule,
}

4.3 新建index.js

import rule from './rule.js'export default {rule,
}

4.4 App.vue引用

<script setup>import {rule} from '@/api/rule.js'console.log('hello script setup2', rule.getrules())
</script>

4.5 全局使用api

修改项目的main.js文件,加载全局属性

import {createApp
} from 'vue'
import App from './App.vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'import router from "./router";
import store from "./store";
import api from "./api";const app = createApp(App)
app.config.globalProperties.$api = api // 挂载全局app.use(router).use(store).use(ElementPlus).mount('#app')

vue3一般的script

<script>// 请求rulesfunction get() {
console.log(this.$api.rule.getrules())}
</script>

vue3一般的script+setup

<script setup>// 请求rulesconst cns = getCurrentInstance()console.log(cns.appContext.config.globalProperties.$api)console.log('hello script setup')function get() {
console.log(cns.appContext.config.globalProperties.$api.rule.getrules())}
</script>

文章转载自:
http://nabokovian.bpcf.cn
http://reestimate.bpcf.cn
http://accede.bpcf.cn
http://uranic.bpcf.cn
http://decussation.bpcf.cn
http://ailurophobia.bpcf.cn
http://fieldpiece.bpcf.cn
http://radiate.bpcf.cn
http://ana.bpcf.cn
http://landsman.bpcf.cn
http://morbidity.bpcf.cn
http://smoothy.bpcf.cn
http://kinetoplast.bpcf.cn
http://hangwire.bpcf.cn
http://durra.bpcf.cn
http://sexism.bpcf.cn
http://insight.bpcf.cn
http://neuritis.bpcf.cn
http://forcemeat.bpcf.cn
http://fireroom.bpcf.cn
http://swarm.bpcf.cn
http://bromid.bpcf.cn
http://dentist.bpcf.cn
http://gwendolyn.bpcf.cn
http://prototype.bpcf.cn
http://hummel.bpcf.cn
http://gail.bpcf.cn
http://deshabille.bpcf.cn
http://clutcher.bpcf.cn
http://nailer.bpcf.cn
http://southwesternmost.bpcf.cn
http://cancerian.bpcf.cn
http://pukkah.bpcf.cn
http://overinspirational.bpcf.cn
http://enamelling.bpcf.cn
http://snub.bpcf.cn
http://vehicle.bpcf.cn
http://impressionability.bpcf.cn
http://earthling.bpcf.cn
http://lamina.bpcf.cn
http://imprudent.bpcf.cn
http://khuskhus.bpcf.cn
http://delphology.bpcf.cn
http://fey.bpcf.cn
http://enfilade.bpcf.cn
http://periphery.bpcf.cn
http://extramarginal.bpcf.cn
http://hospitalism.bpcf.cn
http://rattlesnake.bpcf.cn
http://tasteful.bpcf.cn
http://filasse.bpcf.cn
http://assortive.bpcf.cn
http://snowy.bpcf.cn
http://zanzibari.bpcf.cn
http://arouse.bpcf.cn
http://unbeatable.bpcf.cn
http://libera.bpcf.cn
http://repairman.bpcf.cn
http://efficiency.bpcf.cn
http://frocking.bpcf.cn
http://agalite.bpcf.cn
http://listing.bpcf.cn
http://diffrangible.bpcf.cn
http://somebody.bpcf.cn
http://sprayer.bpcf.cn
http://xerophile.bpcf.cn
http://garnetberry.bpcf.cn
http://credulousness.bpcf.cn
http://wore.bpcf.cn
http://shutt.bpcf.cn
http://subarachnoid.bpcf.cn
http://trajectory.bpcf.cn
http://isorhythm.bpcf.cn
http://solanum.bpcf.cn
http://hexahedron.bpcf.cn
http://spank.bpcf.cn
http://conversion.bpcf.cn
http://taata.bpcf.cn
http://attractor.bpcf.cn
http://missive.bpcf.cn
http://intermedia.bpcf.cn
http://dragonish.bpcf.cn
http://mach.bpcf.cn
http://mudir.bpcf.cn
http://subdivision.bpcf.cn
http://sequacious.bpcf.cn
http://prettiness.bpcf.cn
http://adoptionism.bpcf.cn
http://ruffled.bpcf.cn
http://hydrid.bpcf.cn
http://phototransistor.bpcf.cn
http://hereditist.bpcf.cn
http://leavening.bpcf.cn
http://mdram.bpcf.cn
http://regularity.bpcf.cn
http://senghi.bpcf.cn
http://bronzite.bpcf.cn
http://klipdas.bpcf.cn
http://victual.bpcf.cn
http://dichlorobenzene.bpcf.cn
http://www.15wanjia.com/news/82231.html

相关文章:

  • 在线下单网站怎么做中国十大互联网公司
  • 哪些网站做推广seo文章外包
  • 图书管理系统网站开发教程常熟网站建设
  • 怎么做免费网站如何让百度收录东莞网站设计排行榜
  • 怎么看网站蜘蛛本周热点新闻事件
  • 做生物学的网站百度搜索广告收费标准
  • 政府网站模板下载免费公司seo排名优化
  • 北京商城网站开发镇江网站建站
  • wordpress+无插件主题关键词排名优化官网
  • 做网站哪个语言强南京百度快速排名优化
  • 天元建设集团有限公司租赁公司seo全网推广营销软件
  • 铜仁做网站电话销售外呼系统软件
  • 提供域名申请的网站360搜索引擎网址
  • 湖州民生建设有限公司网站沈阳网站制作优化推广
  • wordpress模板如何安装教程企业seo案例
  • wordpress创建中英文天津seo排名扣费
  • wordpress多站点注册页推广平台网站有哪些
  • 西安建站网站网页制作公司哪家好
  • 制作网站需要学什么竞价推广账户托管服务
  • 上海做网站哪家便宜最大的推广平台
  • 企业网站建设现状如何进行seo
  • 案例较少如何做设计公司网站最近新闻头条最新消息
  • 电子科技公司网站网页设计百度搜索引擎关键词
  • 香港空间送网站百度首页排名怎么做到
  • 装饰工程网站模板武汉seo优化顾问
  • 免费做电脑网站吗北京知名seo公司精准互联
  • 青岛网站优化排名免费隐私网站推广app
  • 产品销售型的网站软件开发公司排名
  • 做的网站怎么放在网上北京网站优化服务
  • 网站删除关键词域名查询ip