【Vue】天气预报功能--axios的使用实例

axios的教程

使用起来挺简单的,看文档再加上实例就能明白了。
文档链接点击这里

axios的安装

进入到项目文件夹下,运行终端,输入npm install axios --save,点击回车。

axios的使用

在哪里使用,在哪里引入。

比如我需要在App.vue页面使用axios,那么就在App.vue页面引入:
在这里插入图片描述

需求

做一个天气预报的页面,显示未来十五天的天气。默认显示地区为广州,支持搜索功能。

静态页面

在这里插入图片描述
将页面的标题抽取出来做成组件CardTitle
将页面中的表格抽取出来做成组件WeatherTable

App.vue
<template>
  <div class="container-fluid">
    <cardtitle :title="cityName" />
    <weathertable :weatherlist="WeatherList" :weathersearch="WeatherSeacrh" />
  </div>
</template>

<script>
  import axios from 'axios' //引入axios
  import CardTitle from './components/CardTitle.vue' //引入组件
  import WeatherTable from './components/WeatherTable.vue'

  export default {
     
    data() {
     
      return {
     
        //通过API获取到的天气数据存放在这个数组里面
        WeatherList: [],
        cityName:'广州'//默认显示广州的天气
      }
    },
    components: {
     
      cardtitle: CardTitle,//将组件映射为标签
      weathertable: WeatherTable
    },
    mounted() {
     	//生命周期钩子
    	this.WeatherSearch('广州');
    },
    methods: {
     
      WeatherSeacrh(cityName) {
     
        const url =
          `https://api.apishop.net/common/weather/get15DaysWeatherByArea?apiKey=您自己的apikey&area=${
       cityName}`;
        //使用axios发送ajax请求获取数据
        axios.get(url).then(response => 
  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值