HarmonyOS 鸿蒙Next 项目构建时报错 Cannot read properties of undefined (reading 'app') 但代码无直接报错,初始数据有空值,或因异步获取数据导致?
HarmonyOS 鸿蒙Next 项目构建时报错 Cannot read properties of undefined (reading ‘app’) 但代码无直接报错,初始数据有空值,或因异步获取数据导致?
//主页面
struct PageVideo {
@State videoArray: Array<VideoItem> = initializeOnStartup();
@State index: number = 0; // video index.
@State pageShow: boolean = false;
build() {
Column() {
Swiper()
.width(CommonConstants.FULL_WIDTH)
.height(CommonConstants.FULL_HEIGHT)
.indicator(false)
.loop(false)
.vertical(true)
.onChange((index: number) => {
this.index = index;
})
.ForEach(this.videoArray, (item: VideoItem, index: number | undefined) => {
PlayView({
index: $index,
pageShow: $pageShow,
item: item,
barPosition: index
});
}, (item: VideoItem) => JSON.stringify(item))
}
}
onPageShow(): void {
this.pageShow = true;
}
onPageHide(): void {
this.pageShow = false;
}
}
export struct PlayView {
private isShow: boolean = false;
@Link @Watch('needPageShow') index: number;
@Link @Watch('needPageShow') pageShow: boolean;
@State item: VideoItem = new VideoItem();
private barPosition: number = 0;
@State private playState: number = PlayState.STOP;
private videoController: VideoController = new VideoController();
build() {
Stack({ alignContent: Alignment.End }) {
Video({
src: this.item.src,
controller: this.videoController
})
.controls(false)
.autoPlay(this.playState === PlayState.START ? true : false)
.objectFit(ImageFit.Fill)
.loop(true)
.height(CommonConstants.WIDTH_VIDEO)
.width(CommonConstants.FULL_WIDTH)
.onClick(() => {
if (this.playState === PlayState.START) {
this.playState = PlayState.PAUSE;
this.videoController.pause();
} else if (this.playState === PlayState.PAUSE) {
this.playState = PlayState.START;
this.videoController.start();
}
})
/*NavigationView()
CommentView({ item: this.item })
DescriptionView()*/
}
.backgroundColor(Color.Black)
.width(CommonConstants.FULL_WIDTH)
.height(CommonConstants.FULL_HEIGHT)
}
onPageSwiperShow(): void {
if (this.playState != PlayState.START) {
this.playState = PlayState.START;
this.videoController.start();
}
}
onPageSwiperHide(): void {
if (this.playState != PlayState.STOP) {
this.playState = PlayState.STOP;
this.videoController.stop();
}
}
needPageShow(): void {
if (this.pageShow === true) {
if (this.barPosition === this.index) { // Judge whether the index is the same as the current location.
this.isShow = true;
this.onPageSwiperShow();
} else {
if (this.isShow === true) { // The already visible status is changed to invisible, and the invisible method callback is triggered.
this.isShow = false;
this.onPageSwiperHide();
}
}
} else { // Stop when the page goes back to the background.
this.isShow = false;
this.onPageSwiperHide();
}
}
}
export const VIDEO_DATA: VideoItem[] = [
{
id: '1',
src: $rawfile('video1.mp4'),
likesCount: 0,
isLikes: false,
commentCount: 102,
shareTimes: 666
},
{
id: '2',
src: $rawfile('video2.mp4'),
likesCount: 8654,
isLikes: true,
commentCount: 0,
shareTimes: 0
}
];
/**
* state of video play.
*/
export enum PlayState {
STOP = 0,
START = 1,
PAUSE = 2
}
更多关于HarmonyOS 鸿蒙Next 项目构建时报错 Cannot read properties of undefined (reading 'app') 但代码无直接报错,初始数据有空值,或因异步获取数据导致?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
楼主您好,
正确的配置文件为app.json5,所以需要排查工程级AppScope内app.json5文件是否存在或者名称是否为app.json5
如下图所示,报错时模块内的配置文件格式错误,导致hvigor无法读取到app.json5中的参数,所以报错提示为无法读取app.json5文件中最外层的app字段。
 但代码无直接报错,初始数据有空值,或因异步获取数据导致?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next项目中,出现Cannot read properties of undefined (reading 'app')
错误,通常是因为在访问app
属性时,该对象未定义或未初始化。以下是一些可能的原因和解决方法:
-
异步数据获取问题:如果
app
属性的值是通过异步操作获取的,可能在访问时数据尚未返回。确保在访问app
属性之前,异步操作已完成,并且数据已正确赋值。 -
初始数据为空值:检查
app
属性的初始值是否为空,如果是空值,确保在使用前进行非空判断。可以使用条件语句或安全导航操作符(如?.
)来避免访问未定义的属性。 -
生命周期问题:在鸿蒙中,组件的生命周期方法(如
onInit
、onReady
等)可能影响数据的初始化时机。确保在正确的生命周期方法中进行数据初始化和访问。 -
依赖注入问题:如果
app
属性是通过依赖注入或其他方式获取的,确保注入的依赖已正确配置,并且在需要时已准备好。 -
代码逻辑错误:检查代码逻辑,确保在访问
app
属性时,相关的对象或变量已正确初始化。避免在未定义或未初始化的状态下访问属性。
总结:该错误通常与数据初始化和异步操作有关,确保在访问app
属性时,相关数据已正确初始化和赋值。
这个错误通常是由于在数据尚未准备好时就尝试访问 app
属性导致的。在鸿蒙Next项目中,异步获取数据时,如果初始数据为空,可能会引发此类问题。建议在访问 app
属性前,先检查数据是否已加载完成。可以使用条件渲染或默认值来避免访问未定义的属性。例如:
if (data && data.app) {
// 安全访问 app 属性
}
确保在数据加载完成后再进行操作,以避免类似错误。