注:鸿蒙的远程交互组件应用相对复杂 ,访问网络时,首先要配置网络权限,华为官方文档有问题,在此引用我老师配置的模板,见附件
过程:1.导入鸿蒙的网络请求模块fetch
2.发起对服务器的请求(在这过程中需要用JSON.parse将括号中的数据转换成json数据格式,具体见代码中标注)
js业务逻辑层
//导入鸿蒙的网络请求模块fetch
import fetch from [email protected];
exportdefault{
data: {
title:‘World‘,
currentTime:‘‘,
temperature1:‘‘,
text:‘‘,
},
onInit() {//发起对心知天气服务器的请求
fetch.fetch({
url:‘https://api.seniverse.com/v3/weather/now.json?key=S0YbU_VLcvXz-4LZx&location=成都&language=zh-Hans&unit=c‘,
responseType:‘json‘,
success:(resp)=>{//JSON.parse(字符串)转换成json数据格式
let weatherInfo=JSON.parse(resp.data);this.currentTime=weatherInfo.results[0].last_update;this.text=weatherInfo.results[0].now.text;this.temperature1=weatherInfo.results[0].now.temperature;
}
});
}
}
渲染层
{{currentTime}}{{temperature1}}
{{temperature1}}
{{text}}