数据获取:
config.json中:
"module": {
"reqPermissions": [
{
"name": "ohos.permission.GET_NETWORK_INFO"
},
{
"name": "ohos.permission.SET_NETWORK_INFO"
},
{
"name": "ohos.permission.INTERNET"
}
],
...
}
"deviceConfig": {
"default": {
"process": "com.huawei.hiworld.example",
"supportBackup": false,
"network": {
"cleartextTraffic": true,
"securityConfig": {
"domainSettings": {
"cleartextPermitted": true,
"domains": [
{
"subDomains": true,
"name": "api.seniverse.com"
}
]
}
}
}
}}
某个JS中:
//导入鸿蒙的网络请求模块fetchimport fetch from '@system.fetch';export default {
data: {
winfo:""
},
onInit() {
//发起对心知天气服务器的网络请求
fetch.fetch({
url:`https://api.seniverse.com/v3/weather/now.json?
key=WNEUXAAE2G&location=南京&language=zh-Hans&unit=c`,
responseType:"json",
success:(resp)=>
{
//JSON.parse(字符串)转换成json数据格式
this.winfo=JSON.parse(resp.data);
console.log(this.winfo)
}
});
}
}
自定义组建:
创建文件夹:
common.datas.tabbarItem.js
放JS:
export default [
{
img:'common/images/home.png',
simg:'common/images/home_s.png',
name:'首页'
},
{
img:'common/images/hot.png',
simg:'common/images/hot_s.png',
name:'热点'
},
{
img:'common/images/us.png',
simg:'common/images/us_s.png',
name:'社区'
},
{
img:'common/images/me.png',
simg:'common/images/me_s.png',
name:'我'
}
]
创建文件夹:
common.components.tabbar
创建:
tabbar.hml、tabbar.js、tabbar.css
hml:
<div class="container">
<toolbar class="tabbar">
<toolbar-item for="{{tabbarItems}}" icon='{{$item.img}}' value='{{$item.name}}' οnclick="jump($idx)" ></toolbar-item>
</toolbar></div>
js:
import tabbarItems from '../../common/datas/tabbarItem.js';export default {
data:{
tabbarItems
},
jump(index){
this.tabbarItems.forEach((item,index) => {
item.img = tabbarItems[index].img;
});
this.tabbarItems[index].img = this.tabbarItems[index].simg;
}
}
css:
.tabbar {
position: fixed;
left: 0;
bottom: 0;
}
调用:
hml:
<element name='comp' src='../../components/tabbar/tabbar.hml'></element><div class="container">
<text class="title">
首页
</text>
<comp></comp></div>