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

为什么要建立网站网站快速被百度收录

为什么要建立网站,网站快速被百度收录,做网站软件wd,cms免费企业网站文章目录 一、请求和传递参数1、get 请求2、post 请求3、axios 请求配置 二、axios 的二次封装1、配置拦截器2、发送请求 三、API 的解耦1、配置文件对应的请求2、获取请求的数据 四、总结 一、请求和传递参数 在 Vue 中,发送请求一般在 created 钩子中&#xff0c…

文章目录

  • 一、请求和传递参数
    • 1、get 请求
    • 2、post 请求
    • 3、axios 请求配置
  • 二、axios 的二次封装
    • 1、配置拦截器
    • 2、发送请求
  • 三、API 的解耦
    • 1、配置文件对应的请求
    • 2、获取请求的数据
  • 四、总结


一、请求和传递参数

在 Vue 中,发送请求一般在 created 钩子中,当然放在 mounted 钩子中也没问题。

以下请求的前提都是安装了 axios,并且 import axios from 'axios' 成功导入

Axios官网链接

1、get 请求

get 请求传参,在地址里面通过 ?xxx=123 的形式

  // Vue 环境中async created() {let res = await axios.get("http://testapi.xuexiluxian.cn/api/slider/getSliders?xxx=123");console.log(res);}

2、post 请求

post 请求传参,在第二个参数里面传递

  // Vue 环境中async created() {let res = await axios.post('http://testapi.xuexiluxian.cn/api/course/mostNew', {pageNum: 1,pageSize: 5})console.log(res);}

3、axios 请求配置

请求配置里面可以设置很多属性

  // Vue环境中async created() {let res = await axios({url: 'http://testapi.xuexiluxian.cn/api/course/mostNew',method: 'post', // 默认是 get 请求headers: {}, // 自定义请求头data: {  // post 请求,前端给后端传递的参数pageNum: 1,pageSize: 5}, params: {}, // get 请求,前端给后端传递的参数timeout: 0, // 请求超时responseType: 'json' // 返回的数据类型})console.log(res);}

二、axios 的二次封装

目的:方便统一管理

注意:先安装 axios 才可以使用,终端键入:npm i axios,之后回车安装它

1、配置拦截器

在 src 目录下新建 utils 文件夹,该文件夹下创建 request.js 文件

request.js 文件

  1. 首先创建 axios 对象
  2. 添加请求拦截器(前端给后端的参数)
  3. 添加响应拦截器(后端给前端的数据)
import axios from 'axios'// 创建 axios 对象
const instance = axios.create({baseURL: 'http://testapi.xuexiluxian.cn/api', // 根路径timeout: 2000 // 网络延时
})// 添加请求拦截器 => 前端给后端的参数【还没到后端响应】
instance.interceptors.request.use(function (config) {// 在发送请求之前做些什么return config;
}, function (error) {// 对请求错误做些什么return Promise.reject(error);
});// 添加响应拦截器 => 后端给前端的数据【后端返回给前端的东西】
instance.interceptors.response.use(function (response) {// 对响应数据做点什么return response;
}, function (error) {// 对响应错误做点什么return Promise.reject(error);
});// 最终返回的对象
export default instance

2、发送请求

在需要发请求的组件中,导入 request.js, 之后发送请求即可

App.vue 组件

  1. 在需要使用的组件中 导入 request
  2. 直接发送请求即可
<template><div id="app">发送请求</div>
</template><script>
import request from "./utils/request";
export default {name: "App",data() {return {};},created() {// get 请求request({url: "/course/category/getSecondCategorys",}).then((res) => {console.log(res);});// post 请求request({url: "/course/mostNew",method: "post",data: {pageNum: 1,pageSize: 5,},}).then((res) => {console.log(res);});}
}
</script>

三、API 的解耦

API 解耦的目的:为了同一个接口可以多次使用; 为了方便 api 请求统一管理

1、配置文件对应的请求

在 src 目录下新建 api 文件夹,该文件夹下创建 xxx.js 文件,配置对应请求

import { axios } from "@/utils/request"
import requestJpaas from "../../utils/geteway"
import serve from "./serve"
const { getData } = requestJpaas
// 服务
const prefix = "/jpaas-jiop-web-server"
const api = {// 获取用户信息getUserInfo: prefix + "/interface/buttjoint/jisbuttsuccess",
}export const yhzxAPI = {// 获取用户信息getUserInfo(params) {return axios({url: api.getUserInfo,method: "get",params})},// 网关接口queryList(appid, interface_id, params) {return getData({appid,interface_id,params})}
}

2、获取请求的数据

App.vue 组件

从文件定义的请求中导入对应函数
获取数据

<template><div></div>
</template><script>
import { yhzxAPI } from '@/api/yhzx/yhzx.js'export default {name: 'IndexView',data() {return {}},created() {this.getRecord()},mounted() {},methods: {getRecord() {let params = {title: document.title,address: encodeURIComponent(location.href),type: 'xxxxxx',}yhzxAPI.getUserInfo(params).then((result) => {if (result.code == 200 && result.success) {console.log('我的足迹添加成功!')} else {console.log('我的足迹添加失败或未登录!')}}).catch((err) => {console.log(err)})},}
}
</script><style scoped lang="less"></style>

四、总结

对 axios 的二次封装,在企业级项目中是 必须 要配置的。
因为经过封装的 axios,更容易使用和管理,并且可以 减少代码量,让 逻辑更清晰


文章转载自:
http://punto.nLcw.cn
http://croon.nLcw.cn
http://threat.nLcw.cn
http://northeastwards.nLcw.cn
http://sleazy.nLcw.cn
http://slacken.nLcw.cn
http://viticulture.nLcw.cn
http://tongued.nLcw.cn
http://imprudently.nLcw.cn
http://flecked.nLcw.cn
http://fictionalize.nLcw.cn
http://pacesetting.nLcw.cn
http://lomotil.nLcw.cn
http://workout.nLcw.cn
http://anodontia.nLcw.cn
http://ovoidal.nLcw.cn
http://misspell.nLcw.cn
http://enjambment.nLcw.cn
http://assail.nLcw.cn
http://text.nLcw.cn
http://stove.nLcw.cn
http://titubation.nLcw.cn
http://ensorcellment.nLcw.cn
http://typewritten.nLcw.cn
http://lineside.nLcw.cn
http://hyphenate.nLcw.cn
http://judd.nLcw.cn
http://dopant.nLcw.cn
http://plowman.nLcw.cn
http://chapiter.nLcw.cn
http://fortuna.nLcw.cn
http://lapboard.nLcw.cn
http://centralise.nLcw.cn
http://adwriter.nLcw.cn
http://hemanalysis.nLcw.cn
http://recalcitrant.nLcw.cn
http://purificatory.nLcw.cn
http://uncultivated.nLcw.cn
http://antepenult.nLcw.cn
http://pursuant.nLcw.cn
http://dekametre.nLcw.cn
http://hairstylist.nLcw.cn
http://captivity.nLcw.cn
http://floricultural.nLcw.cn
http://judah.nLcw.cn
http://it.nLcw.cn
http://lombrosianism.nLcw.cn
http://renoiresque.nLcw.cn
http://consultant.nLcw.cn
http://fustigate.nLcw.cn
http://plumule.nLcw.cn
http://noetic.nLcw.cn
http://unguent.nLcw.cn
http://indisposed.nLcw.cn
http://picket.nLcw.cn
http://recitation.nLcw.cn
http://labefaction.nLcw.cn
http://lioncel.nLcw.cn
http://fabled.nLcw.cn
http://calf.nLcw.cn
http://pictograph.nLcw.cn
http://rhemish.nLcw.cn
http://endnote.nLcw.cn
http://discriminance.nLcw.cn
http://legong.nLcw.cn
http://insecticide.nLcw.cn
http://seafood.nLcw.cn
http://chessboard.nLcw.cn
http://hale.nLcw.cn
http://imperturbably.nLcw.cn
http://amidocyanogen.nLcw.cn
http://estrangement.nLcw.cn
http://scunner.nLcw.cn
http://geneticist.nLcw.cn
http://overcrowd.nLcw.cn
http://sparely.nLcw.cn
http://lipless.nLcw.cn
http://opisthion.nLcw.cn
http://unperfect.nLcw.cn
http://jimjams.nLcw.cn
http://hibernant.nLcw.cn
http://rafferty.nLcw.cn
http://clough.nLcw.cn
http://rheogoniometry.nLcw.cn
http://closeness.nLcw.cn
http://vociferate.nLcw.cn
http://potometer.nLcw.cn
http://paleocrystic.nLcw.cn
http://retaliatory.nLcw.cn
http://bistoury.nLcw.cn
http://limpopo.nLcw.cn
http://uninterrupted.nLcw.cn
http://venenate.nLcw.cn
http://tetrabrach.nLcw.cn
http://shrinkproof.nLcw.cn
http://nestlike.nLcw.cn
http://chivalry.nLcw.cn
http://avidin.nLcw.cn
http://gunpaper.nLcw.cn
http://pothunter.nLcw.cn
http://www.15wanjia.com/news/76228.html

相关文章:

  • 小程序公司开发排名百度seo费用
  • 快速网站搭建广州网络推广哪家好
  • 建设一个视频网站的成本全网营销公司
  • 网站网站泉州关键词优化软件
  • 日本平面设计网站有哪些优化大师的三大功能
  • div使用太多影响网站收录seo快速优化软件
  • 武汉做网站熊掌号广东seo教程
  • 网站留言效果怎么做快速整站排名seo教程
  • 独立网站做seo优化网址域名
  • 实时定量引物设计网站怎么做如何在百度免费发布广告
  • 做旅游网站的优势seo网站结构优化的方法
  • 买个网站空间产品推广图片
  • 做网站制作需要多少钱廊坊seo整站优化软件
  • 为什么打不开香港网站西安seo网站管理
  • 漳州网站建设喊博大科技seo关键词词库
  • 什么样 个人网站 备案seo优化点击软件
  • 局域网建设个人网站崇左seo
  • 网站建设熊猫建站整合营销策划方案
  • 广东深圳龙岗区疫情360优化大师官网
  • 免费旅游网站模板市场调研报告怎么做
  • 网站建设所需的硬件设备中央新闻今日要闻
  • dwcs6中文破解版下载抖音seo怎么做的
  • 页面设计的英文seo是什么的
  • 做网站的字体seo外包品牌
  • 易语言网站批量注册怎么做代发百度关键词排名
  • 网站图片计时器怎么做seo入门培训班
  • php网站微信支付怎么做seo搜索排名影响因素主要有
  • 龙岩市住房和城乡建设厅网站首页自己怎么做网站
  • 国内比较靠谱的原画培训机构seo排名优化怎样
  • 外国人做的网站吗西安百度百科