如何使用Vue.js访问API?

8

我是JavaScript和Vue.js的新手,我在使用Vue.js访问API时遇到了麻烦。我尝试访问的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
}

API链接本身是有效的,但我认为在运行程序时没有访问它。即使我不尝试解析JSON,只显示从API收集的所有数据,我的变量仍然为空。所以,我一定是在访问API时出了问题。此外,在访问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 代码:

<!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>
2个回答

9

我看到了关于 this 作用域的问题,在 $http.get 块内部,this 的作用域会发生改变,需要进行以下更改:

    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;
                }
        }
    }

您也可以查看我类似的答案,在这里

谢谢,它仍然没有工作,但那会在以后给我带来问题。 - thefreebird777
@thefreebird777 你是从API获取数据吗?如果你在$http调用中使用console.log输出数据的话? - Saurabh
不是的。但是如果我将API网址放在地址栏中,它可以工作,所以链接是正确的。 - thefreebird777
你能否在 $http 调用内部 console.log(data.main) 并检查获取的内容? - Saurabh
(进程:1374):GLib-CRITICAL **:g_path_get_basename:断言'file_name!= NULL'失败 - thefreebird777

3

我希望在你的代码中使用Promise和进行一些其他调整。

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()
                          })
            }
        }

    })
    ;

应该是这样的:this.data.getTemp = response.data吗? - kiltek
4
不可以,它不应该。 - Belmin Bedak
1
好的,它神奇地自己解决了。使用你的版本使其正常工作。 - kiltek
它是如何算出来的呢?@BelminBedak - adelriosantiago
@adelriosantiago 不太确定,但我猜它使用某种代理。 - Belmin Bedak

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接