Swiper组件的点击事件不生效,应该如何处理?
问题代码示例参考如下:
import { PromptAction } from '@kit.ArkUI';
@Entry
@ComponentV2
struct Index {
@Local message: string = 'Hello World';
swiperController: SwiperController = new SwiperController();
imaglist: string[] = [
'www.example.com',
'www.example.com',
'www.example.com',
'www.example.com'
]
promptAction: PromptAction = this.getUIContext().getPromptAction();
build() {
RelativeContainer() {
Swiper(this.swiperController) {
ForEach(this.imaglist, (imagurl: string, index: number) => {
Image(imagurl)
.width('100%')
.height(300)
.expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP])
.onClick(() => {
this.promptAction.showToast({
message: '第' + index + '张',
duration: 1500,
bottom: "center",
})
})
})
}
.id("swiper")
.clip(false)
.autoPlay(true)
.height(280)
Column() {
Text(this.message)
.id('HelloWorld')
.fontSize($r('app.float.page_text_font_size'))
.fontWeight(FontWeight.Bold)
.onClick(() => {
this.message = 'Welcome';
this.promptAction.showToast({
message: '轮播图无法点击\n错误示范',
duration: 1500,
bottom: "center",
})
})
}
.width('100%')
.height('100%')
.justifyContent(FlexAlign.Center)
}
.height('100%')
.width('100%')
}
} 问题效果预览:

import { PromptAction } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@ComponentV2
struct Index {
message: string = 'Hello World';
swiperController: SwiperController = new SwiperController();
// www.example.com仅做示例,需要替换为有效的图片url/resource
imageList: string[] = [
'www.example.com',
'www.example.com',
'www.example.com'
];
promptAction: PromptAction = this.getUIContext().getPromptAction();
build() {
RelativeContainer() {
// swiper component
Swiper(this.swiperController) {
ForEach(this.imageList, (imageUrl: string, index: number) => {
Image(imageUrl)
.width('100%')
.height(300)
.expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP])
.onClick(() => {
try {
this.promptAction.showToast({
message: '第' + index + '张',
duration: 1500,
bottom: 'center',
});
} catch (error) {
let message = (error as BusinessError).message;
let code = (error as BusinessError).code;
console.error(`showToast args error code is ${code}, message is ${message}`);
}
});
});
}
.id('swiper')
.clip(false)
.autoPlay(true)
.height(280);
Column() {
Text(this.message)
.id('HelloWorld')
.fontSize($r('app.float.page_text_font_size'))
.fontWeight(FontWeight.Bold)
.onClick(() => {
this.message = 'Welcome';
try {
this.promptAction.showToast({
message: '轮播图无法点击\n错误示范',
duration: 1500,
bottom: 'center',
});
} catch (error) {
let message = (error as BusinessError).message;
let code = (error as BusinessError).code;
console.error(`showToast args error code is ${code}, message is ${message}`);
}
});
}
.hitTestBehavior(HitTestMode.Transparent)
.width('100%')
.height('100%')
.justifyContent(FlexAlign.Center);
}
.height('100%')
.width('100%');
}
} 效果预览:

import { PromptAction } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@ComponentV2
struct Index {
@Local message: string = 'Hello World';
swiperController: SwiperController = new SwiperController();
// www.example.com仅做示例,需要替换为有效的图片url/resource
imageList: string[] = [
'www.example.com',
'www.example.com',
'www.example.com'
];
promptAction: PromptAction = this.getUIContext().getPromptAction();
build() {
RelativeContainer() {
Swiper(this.swiperController) {
ForEach(this.imageList, (imageUrl: string, index: number) => {
Image(imageUrl)
.width('100%')
.height(300)
.expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP])
.onClick(() => {
try {
this.promptAction.showToast({
message: `第${index}张`,
duration: 1500,
bottom: 'center',
});
} catch (error) {
let message = (error as BusinessError).message;
let code = (error as BusinessError).code;
console.error(`showToast args error code is ${code}, message is ${message}`);
}
});
});
}
.id('swiper')
.clip(false)
.autoPlay(true)
.height(280);
Column() {
Text(this.message)
.id('HelloWorld')
.fontSize($r('app.float.page_text_font_size'))
.fontWeight(FontWeight.Bold)
.onClick(() => {
this.message = 'Welcome';
try {
this.promptAction.showToast({
message: '轮播图无法点击\n错误示范',
duration: 1500,
bottom: 'center',
});
} catch (error) {
let message = (error as BusinessError).message;
let code = (error as BusinessError).code;
console.error(`showToast args error code is ${code}, message is ${message}`);
}
});
}
.alignRules({
'top': { 'anchor': 'swiper', 'align': VerticalAlign.Bottom },
'left': { 'anchor': 'swiper', 'align': HorizontalAlign.Start }
})
.width('100%')
.height('100%')
.justifyContent(FlexAlign.Center);
}
.height('100%')
.width('100%');
}
} 效果预览:
