vue3+ts+vite uniapp 怎么获取当前页面标题 以及 当前页面是否为自定义头部导航
l***@163.com
- 发布:2024-09-27 10:44
- 更新:2024-10-16 17:58
- 阅读:661
// 微信小程序获取当前页面配置
getWxCurretPageTitle(route) {
if (route === '未取到' || !route) {
return '';
}
let title = '';
try {
if (__wxConfig) {
let wxConfig = __wxConfig;
let page_list = __wxConfig.page || {};
let currentPageConfig = page_list[route] || page_list[route + '.html'];
let globalConfigTitle = {},
pageConfigTitle = {};
if (wxConfig.global && wxConfig.global.window && wxConfig.global.window.navigationBarTitleText) {
globalConfigTitle.titleVal = wxConfig.global.window.navigationBarTitleText;
}
if (currentPageConfig && currentPageConfig.window && currentPageConfig.window.navigationBarTitleText) {
pageConfigTitle.titleVal = currentPageConfig.window.navigationBarTitleText;
}
if (!pageConfigTitle.titleVal && __wxAppCode__) {
let page_config = __wxAppCode__[route + '.json'];
if (page_config && page_config['navigationBarTitleText']) {
pageConfigTitle.titleVal = page_config['navigationBarTitleText'];
}
}
if (title.length === 0) {
var finalTitle = _.extend(globalConfigTitle, pageConfigTitle);
title = finalTitle.titleVal || '';
}
}
} catch (err) {
console.error(err);
}
return title;
},
靐齉齾麤龖龗 - 解决不了问题,那就解决提出问题的人
const page = getCurrentPages().at(-1)
// 标题文字,仅支持page.json里面设置的或者使用uni.setNavigationBarTitle设置的,自定义的需要你自己获取
const title = page.$page.meta.navigationBar.titleText
// 是否是自定义导航栏
const isCustom = page.$page.meta.navigationBar.style === 'custom'
微信小程序 端获取页面标题
getWxCurretPageTitle(route) {
let title = '';
try {
if (__wxConfig) {
let wxConfig = __wxConfig;
let page_list = __wxConfig.page || {};
let currentPageConfig = page_list[route] || page_list[route + '.html'];
let globalConfigTitle = {},
pageConfigTitle = {};
if (wxConfig.global && wxConfig.global.window && wxConfig.global.window.navigationBarTitleText) {
globalConfigTitle.titleVal = wxConfig.global.window.navigationBarTitleText;
}
if (currentPageConfig && currentPageConfig.window && currentPageConfig.window.navigationBarTitleText) {
pageConfigTitle.titleVal = currentPageConfig.window.navigationBarTitleText;
}
if (!pageConfigTitle.titleVal && __wxAppCode__) {
let page_config = __wxAppCode__[route + '.json'];
if (page_config && page_config['navigationBarTitleText']) {
pageConfigTitle.titleVal = page_config['navigationBarTitleText'];
}
}
if (title.length === 0) {
var finalTitle = this.extendObj(globalConfigTitle, pageConfigTitle);
title = finalTitle.titleVal || '';
}
}
} catch (err) {
console.error(err);
}
return title;
},
APP 端获取页面标题
getAPPCurretPageTitle(route) {
if (__uniRoutes) {
const curPageJson = __uniRoutes.find(el=>el.path === `/${route}`)
if (curPageJson) {
return curPageJson.window.navigationBarTitleText
}
return ''
}
return ''
}
条件编译获取
const pages = getCurrentPages()
const currentPage = pages[pages.length - 1]
// #ifdef APP
return this.getAPPCurretPageTitle(currentPage.route)
// #endif
// #ifdef MP
return this.getWxCurretPageTitle(currentPage.route);
// #endif
-
eachObj(obj, iterator, context) {
if (obj == null) {
return false;
}
let breaker = {};
let nativeForEach = Array.prototype.forEach;
if (nativeForEach && obj.forEach === nativeForEach) {
obj.forEach(iterator, context);
} else if (obj.length === +obj.length) {
for (let i = 0, l = obj.length; i < l; i++) {
if (i in obj && iterator.call(context, obj[i], i, obj) === breaker) {
return false;
}
}
} else {
for (let item in obj) {
if (hasOwnProperty.call(obj, item)) {
if (iterator.call(context, obj[item], item, obj) === breaker) {
return false;
}
}
}
}
},
extendObj(obj) {
let slice = Array.prototype.slice;
this.eachObj(slice.call(arguments, 1), function(source) {
for (let prop in source) {
if (source[prop] !== void 0) {
obj[prop] = source[prop];
}
}
});
return obj;
},2024-12-05 09:54
Beach
TypeError: this.extendObj is not a function
2024-11-29 18:01
糖丶宝可梦
回复 Beach: 下面有完整代码
2024-12-05 09:55