Commit 918f99ca authored by 李维's avatar 李维

添加页面生命周期,优化页面加载性能

beforeMounted,mounted
parent e609166a
......@@ -13731,7 +13731,7 @@
"node": {
"__id__": 298
},
"_enabled": false,
"_enabled": true,
"alignMode": 1,
"_target": {
"__id__": 296
......@@ -13857,7 +13857,7 @@
"node": {
"__id__": 301
},
"_enabled": false,
"_enabled": true,
"alignMode": 1,
"_target": {
"__id__": 296
......@@ -18416,7 +18416,7 @@
"node": {
"__id__": 403
},
"_enabled": false,
"_enabled": true,
"alignMode": 1,
"_target": {
"__id__": 401
......@@ -18542,7 +18542,7 @@
"node": {
"__id__": 406
},
"_enabled": false,
"_enabled": true,
"alignMode": 1,
"_target": {
"__id__": 401
......
......@@ -187,7 +187,6 @@ export default class NewClass extends cc.Component {
userInfo;
async getUserInfo() {
const userInfo = await this.Viewer.getComponent("PageViewer").getUserInfo();
console.log(userInfo)
this.userInfo = userInfo.detail;
// 如果是匿名登录 隐藏右上角头像按钮
cc.find("/UserInfo", this.node).active = this.userInfo.isAnonymous ? true : false;
......
......@@ -58,9 +58,11 @@ export default class NewClass extends cc.Component {
async start() {
this.initBg()
this.userInfo = await this.Viewer.getComponent("PageViewer").getUserInfo();
// 注册PageViwer发来的mounted事件[页面已经切换完毕]
this.node.on("mounted", this.mounted.bind(this))
}
async onEnable() {
async mounted() {
this.loading.active = true;
this.scrollView.opacity = 0;
......
......@@ -6,6 +6,17 @@
// - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
import { asyncCallNetworkApiGet } from "./util";
const PageMap = {
LoginPage: "loginPage",
HomePage: "homePage",
LibraryPage: "libraryPage",
LibraryReadPage: "libraryReadPage",
LibraryAudioPage: "libraryAudioPage",
LibraryVideoPage: "libraryVideoPage",
LibraryReadDetailPage: "libraryReadDetailPage",
UnitPage: "unitPage",
MissionPage: "missionPage",
}
const {ccclass, property} = cc._decorator;
......@@ -114,24 +125,8 @@ export default class NewClass extends cc.Component {
async start () {
const userInfo = await this.getUserInfo();
if (!userInfo.phone) {
this._currentViewNode = this.loginPage;
this._currentViewName = "LoginPage"
} else {
this._currentViewNode = this.homePage;
this._currentViewName = "HomePage";
}
// this._currentViewNode = this.missionPage;
// this._currentViewName = "MissionPage"
this._currentViewNode.active = true;
if (userInfo.phone){
this.justShowView("homePage")
} else {
this.justShowView("LoginPage")
}
// 初始化页面
!userInfo.phone ? this.toView("LoginPage") : this.toView("HomePage")
}
_currentViewName = ""
......@@ -141,54 +136,20 @@ export default class NewClass extends cc.Component {
cc.log(`重复跳转页面: ${this._currentViewName}`);
return
}
switch(viewName) {
case 'LoginPage':
await this.switchView(this.loginPage, this._currentViewNode, aniType);
this._currentViewName = 'LoginPage';
this._currentViewNode = this.loginPage;
break;
case 'HomePage':
await this.switchView(this.homePage, this._currentViewNode, aniType);
this._currentViewName = 'HomePage';
this._currentViewNode = this.homePage;
break;
case 'LibraryPage':
await this.switchView(this.libraryPage, this._currentViewNode, aniType);
this._currentViewName = 'LibraryPage';
this._currentViewNode = this.libraryPage;
break;
case 'LibraryReadPage':
await this.switchView(this.libraryReadPage, this._currentViewNode, aniType);
this._currentViewName = 'LibraryReadPage';
this._currentViewNode = this.libraryReadPage;
break;
case 'LibraryAudioPage':
await this.switchView(this.libraryAudioPage, this._currentViewNode, aniType);
this._currentViewName = 'LibraryAudioPage';
this._currentViewNode = this.libraryAudioPage;
break;
case 'LibraryVideoPage':
await this.switchView(this.libraryVideoPage, this._currentViewNode, aniType);
this._currentViewName = 'LibraryVideoPage';
this._currentViewNode = this.libraryVideoPage;
break;
case 'LibraryReadDetailPage':
await this.switchView(this.libraryReadDetailPage, this._currentViewNode, aniType);
this._currentViewName = 'LibraryReadDetailPage';
this._currentViewNode = this.libraryReadDetailPage;
break;
case 'UnitPage':
await this.switchView(this.unitPage, this._currentViewNode, aniType);
this._currentViewName = 'UnitPage';
this._currentViewNode = this.unitPage;
break;
case 'MissionPage':
await this.switchView(this.missionPage, this._currentViewNode, aniType);
this._currentViewName = 'MissionPage';
this._currentViewNode = this.missionPage;
break;
if(!this[PageMap[viewName]]) {
cc.log(`没有找到viewName: ${viewName}`)
return
}
// 触发页面beforeMounted生命周期
this[PageMap[viewName]].emit("beforeMounted");
// 切换页面
await this.switchView(this[PageMap[viewName]], this._currentViewNode, aniType);
this._currentViewName = viewName;
this._currentViewNode = this[PageMap[viewName]];
// mounted
this._currentViewNode.emit("mounted")
}
switchView(inPage, outPage, aniType) {
......@@ -259,7 +220,7 @@ export default class NewClass extends cc.Component {
}
justShowView(viewName) {
this.loginPage.active = viewName == "LoginPage"
this.loginPage.active = viewName == "loginPage"
this.homePage.active = viewName == "homePage"
this.libraryPage.active = viewName == "libraryPage"
this.libraryReadPage.active = viewName == "libraryReadPage"
......
......@@ -42,9 +42,20 @@ export default class NewClass extends cc.Component {
async start() {
this.initBg();
this.listItem = cc.find("view/content/item", this.unitScrollView);
this.node.on("beforeMounted", this.beforeMounted.bind(this))
// 注册PageViwer发来的mounted事件[页面已经切换完毕]
this.node.on("mounted", this.mounted.bind(this))
}
async onEnable() {
async beforeMounted() {
const svNode = this.unitScrollView;
const sv = svNode.getComponent(cc.ScrollView);
const content = sv.content;
content.removeAllChildren();
}
// 监听页面挂载时间
async mounted() {
await this.initView();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment