HarmonyOS鸿蒙Next中map.get()是不是不能传入动态参数
HarmonyOS鸿蒙Next中map.get()是不是不能传入动态参数
场景是根据不同的信息类型articleType去加载不同的组件,如果是this.factoryMap.get(1012)是可以获取到值的,item.articleType就不行,如何解决
更多关于HarmonyOS鸿蒙Next中map.get()是不是不能传入动态参数的实战教程也可以访问 https://www.itying.com/category-93-b0.html
我这边仿照写了demo测试是可以的,楼主确认下你的item.articleType数据类型是否正确(number),数值是否正确。
更多关于HarmonyOS鸿蒙Next中map.get()是不是不能传入动态参数的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
类型没有错,是number,
姓名: 张三
职位: 软件工程师
简介: 拥有超过10年的软件开发经验,擅长Java和Python编程。
import { LoadingHUD } from ‘@ohos/common’
import { requestChannelList } from ‘@ohos/common/src/main/ets/api/channels’
import { ArticleListItem } from ‘@ohos/common/src/main/ets/model/ChannelList’
import { ItemStyle1012 } from ‘…/components/ItemStyle1012’;
import { ItemStyle1007 } from ‘…/components/ItemStyle1007’;
import { ItemStyle1010 } from ‘…/components/ItemStyle1010’;
import { ItemStyle0 } from ‘…/components/ItemStyle0’;
@Component
export struct MixListWithPageLoading {
@Prop channelId: string = “”
@State current_idx: string = “0”
@State isLoading: boolean = true
@State isLoadMore: boolean = false
@State isLoadingMore: boolean = false
@State refreshing: boolean = false;
@State refreshOffset: number = 0;
@State refreshState: RefreshStatus = RefreshStatus.Inactive;
@State canLoad: boolean = false;
@State channelListData:ArticleListItem[] = [];
@State factoryMap:Map<number,object> = new Map()
getNewsList() {
requestChannelList({
currentIdx: this.current_idx,
channelId:this.channelId
}).then(res => {
this.isLoading = false
this.current_idx = res.data.lastCurrentIdx
if(this.isLoadMore){
this.isLoadingMore = false;
this.channelListData = this.channelListData.concat(res.data.items)
}
else{
this.refreshing = false;
this.channelListData = res.data.items
}
})
.catch(()=>{
this.isLoading = false
})
}
aboutToAppear(): void {
this.getNewsList()
this.factoryMap.set(1012, wrapBuilder(Item1012))
this.factoryMap.set(1007, wrapBuilder(Item1007))
}
build() {
Stack() {
Refresh({ refreshing: $$this.refreshing, builder: this.refreshBuilder() }) {
List() {
ForEach(this.channelListData, (item: ArticleListItem) => {
ListItem() {
(this.factoryMap.get(item.articleType) as WrappedBuilder<[ArticleListItem]>).builder(item);
// this.ItemLayout(item)
}
}, (item: ArticleListItem) => JSON.stringify(item))
ListItem() {
this.footer();
}
}
.onScrollIndex((start: number, end: number) => {
// 当达到列表末尾时,触发新数据加载
if (this.canLoad && end >= this.channelListData.length - 1) {
this.canLoad = false;
this.isLoadingMore = true;
this.isLoadMore = true
this.getNewsList()
}
})
.onScrollFrameBegin((offset: number, state: ScrollState) => {
// 只有当向上滑动时触发新数据加载
if (offset > 5 && !this.isLoading) {
this.canLoad = true;
}
return { offsetRemain: offset };
})
.scrollBar(BarState.Off)
// 开启边缘滑动效果
.edgeEffect(EdgeEffect.Spring, { alwaysEnabled: true })
}
.width('100%')
.height('100%')
.onOffsetChange((offset: number) => {
this.refreshOffset = offset;
})
.onStateChange((state: RefreshStatus) => {
this.refreshState = state;
})
.onRefreshing(() => {
// 模拟数据刷新
this.current_idx = "0"
this.isLoadMore = false
this.getNewsList()
})
if (this.isLoading) {
LoadingHUD({isText:false,loadSize:40})
.width("100%")
.height("100%")
}
}
.padding({left:$r('app.float.page_col_padding_sm'),right:$r('app.float.page_col_padding_sm')})
.width("100%")
.height("100%")
}
@Builder
footer() {
Row() {
LoadingProgress().height(32).width(48)
Text(“加载中”)
}.width(“100%”)
.height(64)
.justifyContent(FlexAlign.Center)
// 当不处于加载中状态时隐藏组件
.visibility(this.isLoadingMore ? Visibility.Visible : Visibility.Hidden)
}
@Builder
ItemLayout(item:ArticleListItem) {
Stack() {
if (item.articleType === 1012) { // 滚动新闻
ListItem() {
ItemStyle1012({ data: item })
}
}
else if(item.articleType === 1007){//轮播图
ListItem() {
ItemStyle1007({ data: item })
}
}
else if(item.articleType === 1010){//金刚位
ListItem() {
ItemStyle1010({ data: item })
}
}
else if(item.articleType === 0){//左文右图
ListItem() {
ItemStyle0({ data: item })
}
}
}
}
@Builder
refreshBuilder() {
Stack({ alignContent: Alignment.Bottom }) {
// 可以通过刷新状态控制是否存在Progress组件
// 当刷新状态处于下拉中或刷新中状态时Progress组件才存在
if (this.refreshState != RefreshStatus.Inactive && this.refreshState != RefreshStatus.Done) {
Progress({ value: this.refreshOffset, total: 64, type: ProgressType.Ring })
.width(32).height(32)
.color($r(‘app.color.default_theme_color’))
.style({ strokeWidth: 10})
.style({ status: this.refreshing ? ProgressStatus.LOADING : ProgressStatus.PROGRESSING })
.margin(10)
}
}
.clip(true)
.height(“100%”)
.width(“100%”)
}
}
@Builder
function Item1012(item:ArticleListItem){
ItemStyle1012({ data: item })
}
@Builder
function Item1007(item:ArticleListItem){
ItemStyle1007({ data: item })
}
在HarmonyOS鸿蒙Next中,map.get()
是可以传入动态参数的。map.get()
方法用于从Map对象中获取与指定键关联的值,键可以是任意类型,包括动态生成的对象或变量。开发者可以根据需要在运行时动态生成键,并将其作为参数传递给map.get()
方法。鸿蒙的Map实现遵循标准的键值对存储和检索机制,支持动态参数的使用。
在HarmonyOS鸿蒙Next中,map.get()
方法是可以传入动态参数的。map.get(key)
方法用于根据键 key
获取对应的值,key
可以是任何类型,包括动态生成的变量或表达式。只要 key
的类型与 map
中键的类型匹配,就能正常使用。如果 key
不存在于 map
中,get()
方法会返回 null
。因此,动态参数的使用是完全支持的。