vue项目里访问第三方接口如天气接口、获取IP接口
vue
vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
项目地址:https://gitcode.com/gh_mirrors/vu/vue
·
天气接口
这里用的是这个:https://www.tianqiapi.com
默认返回当前IP地区天气
请求接口:
getDateWeatherAir () {
return $http({
url: `https://www.tianqiapi.com/参数`,
method: 'get',
});
},
数据举例:
方法:
store.getDateWeatherAir(ip).then(res => {
if (res) {
this.weather.today = res.data[0];
this.weather.tmr = res.data[1];
}
});
IP
获取IP地址,可以用搜狐的这个
http://pv.sohu.com/cityjson?ie=utf-8

1、index.html里面写script
在index.html中直接写
<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
<script type="text/javascript">
sessionStorage.setItem('ip', returnCitySN["cip"]);
</script>
在页面中直接取let ip = sessionStorage.getItem('ip');
这种方法不是很推荐,会导致在每个页面都访问该外部接口
2、用js间接写。。
export default function getIP () {
let script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'http://pv.sohu.com/cityjson?ie=utf-8';
document.head.appendChild(script);
let script2 = document.createElement('script');
script2.type = 'text/javascript';
script2.innerText = 'sessionStorage.setItem(\'ip\', returnCitySN["cip"]);';
document.head.appendChild(script2);
}
但是容易报错returnCitySN is not defined
vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
最近提交(Master分支:2 个月前 )
9e887079
[skip ci] 1 年前
73486cb5
* chore: fix link broken
Signed-off-by: snoppy <michaleli@foxmail.com>
* Update packages/template-compiler/README.md [skip ci]
---------
Signed-off-by: snoppy <michaleli@foxmail.com>
Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com> 1 年前
新一代开源开发者平台 GitCode,通过集成代码托管服务、代码仓库以及可信赖的开源组件库,让开发者可以在云端进行代码托管和开发。旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。
更多推荐


所有评论(0)