简体   繁体   English

如何使用Vue.js访问API?

[英]How to access an API with Vue.js?

I'm new to JavaScript and Vue.js, and I'm having trouble accessing an api using Vue.js. 我是JavaScript和Vue.js的新手,我在使用Vue.js访问api时遇到了问题。 The API I'm trying to access has JSON that looks like this: 我正在尝试访问的API具有如下所示的JSON:

{
    "coord": {
        "lon": -88.99,
        "lat": 40.51
    },
    "weather": [
        {
            "id": 800,
            "main": "Clear",
            "description": "clear sky",
            "icon": "01n"
        }
    ],
    "base": "stations",
    "main": {
        "temp": 2.09,
        "pressure": 1022.3,
        "humidity": 69,
        "temp_min": 2.09,
        "temp_max": 2.09,
        "sea_level": 1052.03,
        "grnd_level": 1022.3
    },
    "wind": {
        "speed": 12.66,
        "deg": 205.502
    },
    "clouds": {
        "all": 0
    },
    "dt": 1482203059,
    "sys": {
        "message": 0.186,
        "country": "US",
        "sunrise": 1482239741,
        "sunset": 1482273134
    },
    "id": 4903780,
    "name": "Normal",
    "cod": 200
}

The API link on it's own works, but I do not think I'm accessing it when I run the program. 它上面的API链接是有效的,但是当我运行程序时,我不认为我正在访问它。 Even when I don't try and parse the JSON and just display all the data collected from the api my variable is still empty. 即使我不尝试解析JSON并只显示从api收集的所有数据,我的变量仍然是空的。 So, I must be doing something wrong to access the api. 所以,我必须做错了才能访问api。 Also, after accessing the api, will parsing it like this work: for example, to access the tag "temp" => "data.main.temp" 此外,在访问api之后,将像这样工作解析它:例如,访问标签“temp”=>“data.main.temp”

var weather = new Vue({
        el: '#weather',

        data: {
            getTemp: ''
        },

        created: function () {
            this.fetchData();
        },        

        methods: {
            fetchData: function () {
                this.$http.get('api.openweathermap.org/data/2.5/weather?q=Normal&units=imperial&APPID=MYAPPID'),
                    function (data) {
                        this.getTemp = data.main.temp;
                    }
            }
        }

    })
    ;

HTML code : HTML代码

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://cdn.jsdelivr.net/vue.resource/1.0.3/vue-resource.min.js"></script>
</head>

<body>
<div id="weather">
    {{getTemp}}
</div> <!--end of weather-->
</body>

<script src="app.js"></script>

</html>

I see the problem with scope of this , scope of this changes inside $http.get black, you need to make following changes: 我看到了this范围的问题, this变化的范围在$http.get black里面,你需要进行以下更改:

    methods: {
        fetchData: function () { 
            var vm = this
            this.$http.get('api.openweathermap.org/data/2.5/weather?q=Normal&units=imperial&APPID=MYAPPID'),
                function (data) {
                    vm.getTemp = data.main.temp;
                }
        }
    }

You can also check my similar answer here . 你也可以在这里查看我的类似答案。

I'd like to go with promises, and few other adjustments here, in your code 我想在你的代码中使用promises,以及其他一些调整

var weather = new Vue({
        el: '#weather',

        data: {
            getTemp: []
        },

        created: function () {
            this.fetchData();
        },        

        methods: {
            fetchData: function () {
                this.$http.get('api.openweathermap.org/data/2.5/weather?q=Normal&units=imperial&APPID=MYAPPID')
                          .then(response => {
                             this.getTemp = response.data
                             // or like this this.getTemp = response.json()
                          })
            }
        }

    })
    ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM