我这个方式是否可行?目前用着没啥问题
import { mapGetters, mapState } from 'vuex'
import { appUtil, pageUtil } from '@/utils'
import { LOGIN_CHECK_WHITE_LIST } from '@/constant/pages'
import dictMixin from '@/mixins/dictMixin'
export default {
mixins: [dictMixin],
data() {
return {
/**
* @type {Record<string, any>}
*/
pageParams: {},
prevPage: {},
route: '',
mixinPageLoading: true
}
},
computed: {
...mapState('user', {
__getUserInfoPromise: 'getUserInfoPromise'
}),
...mapGetters('user', {
__userInfo: 'userInfo',
__token: 'token'
}),
mixinPageType() {
let mpType = this.mpType
// #ifdef APP-PLUS
if (mpType === undefined) {
mpType = 'component'
}
// #endif
// #ifdef MP
if (mpType === undefined) {
mpType = 'app'
}
// #endif
return mpType
}
},
beforeCreate() {
const oldOnLoad = this.$options.onLoad || []
const oldCreated = this.$options.created || []
const oldOnShow = this.$options.onShow || []
this.$options.onLoad = oldOnLoad.map((item, index) => {
return async function (e) {
this.__setPageData(e)
if (index === 0) {
await this.__getDict()
} else {
await this.$nextTick()
await this.__getDict()
}
this.__onLoadPromiseResolve = null
this.__onLoadPromise = new Promise((resolve) => {
this.__onLoadPromiseResolve = resolve
})
item.call(this, e)
await this.$nextTick()
this.__onLoadPromiseResolve()
}
})
this.$options.onShow = oldOnShow.map((item) => {
return async function (e) {
if (this.mixinPageType === 'page') {
try {
const _pages = pageUtil.getPages()
const route = _pages[_pages.length - 1].route
const isWhiteListPage = LOGIN_CHECK_WHITE_LIST.includes(`/${route}`)
if (!isWhiteListPage) {
await this.__getUserInfoPromise
}
} catch (err) {
console.log(err)
}
await this.mixinDictHasReady
await appUtil.nextTick()
if (this.__onLoadPromise) {
await this.__onLoadPromise
}
}
await item.call(this, e)
}
})
this.$options.created = oldCreated.map((item, index) => {
return async function (e) {
if (this.mixinPageType === 'page') {
try {
const _pages = pageUtil.getPages()
const route = _pages[_pages.length - 1].route
const isWhiteListPage = LOGIN_CHECK_WHITE_LIST.includes(`/${route}`)
if (!isWhiteListPage) {
await this.__getUserInfoPromise
}
} catch (err) {
console.log(err)
}
}
if (this.mixinPageType === 'component') {
if (this.$options.dictKey?.length) {
if (index === 0) {
try {
await this.__getDict()
} catch (err) {
console.log(err)
}
} else {
await this.mixinDictHasReady
}
}
}
item.call(this, e)
}
})
},
async onLoad() {},
methods: {
__setPageData(e) {
this.pageParams = e || {}
const _pages = pageUtil.getPages()
this.prevPage = _pages.length > 1 ? _pages[_pages.length - 2] : _pages[_pages.length - 1]
this.route = _pages[_pages.length - 1].route
},
mixinClosePageLoading() {
this.mixinPageLoading = false
}
// __getAllDictKeys(vm, dictKeys = []) {
// let { dictKey = [] } = vm.$options || {}
// dictKeys.push(...dictKey)
// const children = vm.$children
// if (children.length) {
// children.forEach((item) => {
// vm.__getAllDictKeys(item, dictKeys)
// })
// }
// return dictKeys
// }
}
}
bigWhite (作者)
那怎么解决 能不能提供个思路
2024-07-01 15:59
bigWhite (作者)
或者怎么达到类似的效果 应该怎么实现
2024-07-01 16:01
DCloud_UNI_yuhe
回复 bigWhite:暂时还找不到实现的方法,如果想要实现类似的效果,可以这样,虽然有点难看,但能实现
2024-07-01 16:08
bigWhite (作者)
回复 DCloud_UNI_yuhe: 那怎么注册一个全局的 就是不管那个页面的onload 都给他挟持
另外你这个方法确实好用
2024-07-01 16:19
DCloud_UNI_yuhe
回复 bigWhite: 确实麻烦,我去看下
2024-07-01 16:26
bigWhite (作者)
回复 DCloud_UNI_yuhe: okok 等你回复
2024-07-01 16:32
bigWhite (作者)
回复 DCloud_UNI_yuhe: 你给的方案 是重写页面的onload 但是当页面原本就有onload方法的时候他俩是 共存的 ,而我想实现的是 在所有页面加载的时候先执行一个方法 (可能是异步方法)等这个方法执行完毕 再去加载页面本身的 onLoad 钩子,感觉有点像三方的统计 不知道他们是怎么实现的?
2024-07-01 16:46