axios的使用以及Vue动画

axios 的使用

Vue.js 2.0 版本推荐使用 axios 来完成 ajax 请求。

Axios 是一个基于 Promise 的 HTTP 库,可以用在浏览器和 node.js 中。

安装方法

使用 npm:

npm install axios

引入

import axios from "axios";

javascript 复制代码
<script>
import {defineComponent} from 'vue'
import axios from "axios";

export default defineComponent({
    name: "axiosView",
    data() {
        return {
            imgSrc: '',
            imgList: [],
            courseList: [],
            boutiqueList: []
        }
    },
    methods: {
        getList(type) {
            return axios.post('https://wkt.myhope365.com/weChat/applet/course/list/type', {
                type,
                pageNum: 1,
                pageSize: 5
            }, {
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                }
            })
        }
    },
    created() {
        // console.dir(axios)
        console.log(this.$axios)
        // get
        axios.get('https://wkt.myhope365.com/weChat/applet/course/banner/list?number=5').then(res => {
            // console.log(res)
            this.imgSrc = res.data.data[0].imgUrlPc
            this.imgList = res.data.data
            // console.log(this.imgList)
        })
        //     post
        let url = new URLSearchParams()   // application/x-www-form-urlencoded
        // let url =new FormData()    formData
        url.append('type', 'free')
        url.append('pageNum', 1)
        url.append('pageSize', 5)

        axios.post('https://wkt.myhope365.com/weChat/applet/course/list/type', url).then(() => {
            // console.log(res)
        })
        // url地址  请求的参数  设置请求头
        // axios.post('https://wkt.myhope365.com/weChat/applet/course/list/type', {
        //     type: 'free',
        //     pageNum: 1,
        //     pageSize: 5
        // }, {
        //     headers: {
        //         'Content-Type': 'application/x-www-form-urlencoded'
        //     }
        // }).then(res => {
        //     this.courseList = res.data.rows
        // })

        // axios.post('https://wkt.myhope365.com/weChat/applet/course/list/type', {
        //     type: 'boutique',
        //     pageNum: 1,
        //     pageSize: 5
        // }, {
        //     headers: {
        //         'Content-Type': 'application/x-www-form-urlencoded'
        //     }
        // }).then(res => {
        //     this.courseList = res.data.rows
        // })

        this.getList('free').then(res => {
            // console.log(res)
            this.courseList = res.data.rows
        })
        this.getList('boutique').then(res => {
            // console.log(res)
            this.boutiqueList = res.data.rows
        })
    }
})
</script>

<template>
    <div>
        <img :src="imgSrc" alt="">
        <!--        <img v-for="item in imgList" :key="item.id" :src="item.imgUrlPc" alt="">-->

        <!--        <div class="box" v-for="item in imgList" :key="item.id"-->
        <!--             :style="`background-image: url(${item.imgUrlPc})`"></div>-->

        <div class="container">
            <div class="box" v-for="(item) in courseList" :key="item.courseId">
                <img :src="item.coverFileUrl"
                     alt="">
                <div>{{ item.courseTitle }}</div>
                <div>共有{{ item.learningNum }}人学习</div>
                <div>免费</div>
            </div>
        </div>


        <div class="container">
            <div class="box" v-for="(item) in boutiqueList" :key="item.courseId">
                <img :src="item.coverFileUrl"
                     alt="">
                <div>{{ item.courseTitle }}</div>
                <div>共有{{ item.learningNum }}人学习</div>
                <div v-if="item.isFree === '1'">免费</div>
                <div v-else-if="item.isDiscount === '1'"> {{item.discountPrice}} <del>{{item.coursePrice}}</del></div>
                <div v-else>{{item.coursePrice}}</div>
            </div>
        </div>
    </div>
</template>

<style scoped lang="less">
.box {
  width: 1000px;
  height: 300px;
  margin: auto;
  background-repeat: no-repeat;
  background-size: cover;
  background-position: 50%;
}

.container {
  width: 1200px;
  margin: auto;
  display: flex;
  justify-content: space-between;

  .box {
    width: 19%;

    img {
      width: 100%;
    }
  }
}
</style>

axios封裝

javascript 复制代码
// 对http请求进行封装
import axios from 'axios'

// 使用自定义的配置文件发送请求
const instance = axios.create({
  baseURL: '',
  timeout: 5000,
  headers: {
  }
});
// 添加请求拦截器
instance.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  return config;
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});

// 添加响应拦截器
instance.interceptors.response.use(function (response) {
  instance// 对响应数据做点什么
  if(response.status === 200){
    return response.data;
  }else{
    console.error("请求错误")
    console.error(response)
  }
  return response;
}, function (error) {
  // 对响应错误做点什么
  return Promise.reject(error);
});

export default instance

Vue动画

使用 transition 将需要过渡的元素包裹起来
​v-enter

​v-enter-active

​v-enter-to

​v-leave

​v-leave-active

​v-leave-to

相关推荐
Maimai1080823 分钟前
React 多步骤表单工程化落地:从 Zod Schema、React Hook Form 到 Zustand 持久化
前端·javascript·react.js·前端框架·状态模式
程序员码歌23 分钟前
我是怎么部署开源 AI 编程助手 OpenCode,并在两个真实场景使用起来的
前端·人工智能·后端
Maimai1080825 分钟前
React Query + Zustand 正确结合方式:不要把接口数据复制进 Store
前端·javascript·react.js·前端框架·web3·状态模式
天才熊猫君27 分钟前
层叠上下文 z-index 的简单理解
前端
i220818 Faiz Ul28 分钟前
智慧养老平台|基于SprinBoot+vue的智慧养老平台系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·毕设·智慧养老平台
AI砖家28 分钟前
每日一个skill:web-artifacts-builder,构建复杂 Claude.ai HTML Artifact 的生产力工具包
java·前端·人工智能·python
icc_tips31 分钟前
Flutter runAppAsync() 详解:干净的异步应用启动
前端·flutter
转转技术团队32 分钟前
AI新名词比我头发掉得还快
前端
Lkstar33 分钟前
Pinia 进阶:Setup Store、插件系统与状态持久化,一篇全搞懂
前端·vue.js
yzin33 分钟前
cjs 和 esm 的差异总结&最佳实践
前端·javascript