0720-13:03
Index.ets
import article from '../pages/article'
import http from '@ohos.net.http';
import HttpTools from './tools/HttpTools';
@Entry
@Component
struct Index
{
@State arr:article[]=
[new article(1,"房东的猫","wechat",10240,5870,4396,"2024-7-19","微信小程序",false),
new article(2,"什码情况","vue",9785,4396,2200,"2024-7-19","todolist",false),
new article(3,"L世界凌乱了","javascript",8888,7777,6666,"2024-7-20","原型",false)]
build()
{
Column()
{
Row()
{
Text("推荐").fontSize(25)
Text("热榜").fontSize(25)
Button("十",{type:ButtonType.Normal}).
onClick((event: ClickEvent) =>
{
})
}.justifyContent(FlexAlign.SpaceBetween)
.width("100%")
//.margin({top:50})
List({ space: 10, initialIndex: 0 })
{
ForEach(this.arr, (item) =>
{
ListItem()
{
Column()
{
Text(item.name).margin({right:250}).fontSize(18)
Text(item.title)
.width("100%")
.margin({top:20,left:0,bottom:100})
.fontSize(25)
.fontWeight(800)
Row()
{
Text(item.viewcounts.toString() + "阅读").fontSize(14)
Text(" | ").fontSize(14)
Text(item.like.toString() + "喜欢").fontSize(14)
Text(" | ").fontSize(14)
Text(item.collect.toString() + "收藏").fontSize(14)
}
.width("100%")
.justifyContent(FlexAlign.Start)
}.width("100%")
}.height(180)
}, item => item.id)
}
.listDirection(Axis.Vertical) // 排列方向
.divider({ strokeWidth: 7, color:('#ffcfcfcb'), startMargin: 0, endMargin: 0 }) // 每行之间的分界线
.edgeEffect(EdgeEffect.Spring) // 滑动到边缘无效果
.onScrollIndex((firstIndex: number, lastIndex: number) =>
{
console.info('first' + firstIndex)
console.info('last' + lastIndex)
})
.width('90%')
}.backgroundColor('#fffff97')
}
login()
{
// 每一个httpRequest对应一个HTTP请求任务,不可复用
let httpRequest = http.createHttp();
let url = ''
let options :http.HttpRequestOptions =
{
method: http.RequestMethod.POST, // 可选,默认为http.RequestMethod.GET
// 开发者根据自身业务需要添加header字段
header:
{
'Content-Type': 'application/json'
},
// 当使用POST请求时此字段用于传递内容
extraData:
{
"data": this.arr,
},
}
HttpTools.get(url,(result)=>{
})
httpRequest.request(url,options, (err, data) =>
{
if (!err)
{
// data.result为HTTP响应内容,可根据业务需要进行解析
console.info('Result:' + JSON.stringify(data.result));
console.info('code:' + JSON.stringify(data.responseCode));
// data.header为HTTP响应头,可根据业务需要进行解析
console.info('header:' + JSON.stringify(data.header));
console.info('cookies:' + JSON.stringify(data.cookies)); // 8+
// 取消订阅HTTP响应头事件
httpRequest.off('headersReceive');
// 当该请求使用完毕时,调用destroy方法主动销毁
httpRequest.destroy();
}
else
{
console.info('error:' + JSON.stringify(err));
// 取消订阅HTTP响应头事件
httpRequest.off('headersReceive');
// 当该请求使用完毕时,调用destroy方法主动销毁。
httpRequest.destroy();
}
})
}
}
HttpTools.ets
export default class HttpTools
{
static get(url:string,fn:Function)
{
}
}