小编the*_*777的帖子

如何使用Vue.js访问API?

我是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
}
Run Code Online (Sandbox Code Playgroud)

它上面的API链接是有效的,但是当我运行程序时,我不认为我正在访问它.即使我不尝试解析JSON并只显示从api收集的所有数据,我的变量仍然是空的.所以,我必须做错了才能访问api.此外,在访问api之后,将像这样工作解析它:例如,访问标签"temp"=>"data.main.temp"

var weather = new Vue({ …
Run Code Online (Sandbox Code Playgroud)

javascript api json vue.js vue-resource

7
推荐指数
1
解决办法
2万
查看次数

标签 统计

api ×1

javascript ×1

json ×1

vue-resource ×1

vue.js ×1