axios的一些东西

<template>
    <div>
        <el-input v-model="scity" placeholder="请输入城市"></el-input>
        <el-button type="primary" @click="getWeatherInfo">获取天气</el-button>
        <el-card>
            {{ city }} &nbsp;&nbsp;&nbsp; {{ wendu }} &nbsp;&nbsp;&nbsp;
            {{ ganmao }}
        </el-card>
        <ul style="list-style: none">
            <li v-for="(item, index) in forecast" :key="index">
                {{ item.date }}{{ item.high }}{{ item.low }}{{ item.type }}
            </li>
        </ul>
    </div>
</template>

<script>
export default {
    name: "SubWeather",
    data() {
        return { scity: "", city: "", wendu: "", ganmao: "", forecast: [] };
    },
    props: [],
    components: {},
    created() {},
    mounted() {},
    methods: {
        getWeatherInfo() {
            let _this = this;
            this.$axios
                .get(`http://wthrcdn.etouch.cn/weather_mini?city=${this.scity}`)
                .then(
                    function (res) {
                        _this.city = res.data.data.city;
                        _this.wendu = res.data.data.wendu;
                        _this.ganmao = res.data.data.ganmao;
                        _this.forecast = res.data.data.forecast;
                    },
                    function (err) {
                        console.log(err);
                    }
                );
        },
    },
};
</script>

<style lang="css" scoped>
</style>

<template>
    <div>
        <el-input v-model="scity" placeholder="请输入城市"></el-input>
        <el-button type="primary" @click="getWeatherInfo">获取天气</el-button>
        <el-card>
            {{ city }} &nbsp;&nbsp;&nbsp; {{ wendu }} &nbsp;&nbsp;&nbsp;
            {{ ganmao }}
        </el-card>
        <ul style="list-style: none">
            <li v-for="(item, index) in forecast" :key="index">
                {{ item.date }}{{ item.high }}{{ item.low }}{{ item.type }}
            </li>
        </ul>
    </div>
</template>

<script>
export default {
    name: "SubWeather",
    data() {
        return { scity: "", city: "", wendu: "", ganmao: "", forecast: [] };
    },
    props: [],
    components: {},
    created() {},
    mounted() {},
    methods: {
        getWeatherInfo() {
            let _this = this;
            this.$axios
                .get("http://wthrcdn.etouch.cn/weather_mini?city="+this.scity)
                .then(
                    function (res) {
                        _this.city = res.data.data.city;
                        _this.wendu = res.data.data.wendu;
                        _this.ganmao = res.data.data.ganmao;
                        _this.forecast = res.data.data.forecast;
                    },
                    function (err) {
                        console.log(err);
                    }
                );
        },
    },
};
</script>

<style lang="css" scoped>
</style>

以上两种写法都可以

注意点 :

.then 回调函数里取不到vue的this实例

解决方法:使用前先赋值给其他变量 let _this = this;

两种不同的url形式

"http://wthrcdn.etouch.cn/weather_mini?city="+this.scity
`http://wthrcdn.etouch.cn/weather_mini?city=${this.scity}`

post

        getInfo() {
            let _this = this;
            this.$axios
                .post("https://autumnfish.cn/api/user/reg", {
                    username: this.uName,
                })
                .then(
                    function (response) {
                        console.log(response.data);
                        _this.msg = response.data;
                    },
                    function (error) {
                        console.log(error);
                    }
                );
        },
Vue学习笔记 文章被收录于专栏

Vue相关

全部评论

相关推荐

点赞 评论 收藏
分享
10-14 21:00
门头沟学院 Java
吃花椒的狸猫:这个人说的倒是实话,特别是小公司,一个实习生哪里来的那么多要求
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务