提交 7faa2657 编写于 作者: Q qiang

fix: 移除 mp-kuaishou 部分冗余代码

上级 4e69bd31
import Vue from 'vue'
import {
initHooks,
initMocks
} from 'uni-wrapper/util'
const hooks = [
'onShow',
'onHide',
'onError',
'onPageNotFound'
]
export default function parseBaseApp (vm, {
mocks,
initRefs
}) {
if (vm.$options.store) {
Vue.prototype.$store = vm.$options.store
}
Vue.prototype.mpHost = __PLATFORM__
Vue.mixin({
beforeCreate () {
if (!this.$options.mpType) {
return
}
this.mpType = this.$options.mpType
this.$mp = {
data: {},
[this.mpType]: this.$options.mpInstance
}
this.$scope = this.$options.mpInstance
delete this.$options.mpType
delete this.$options.mpInstance
if (this.mpType !== 'app') {
initRefs(this)
initMocks(this, mocks)
}
}
})
const appOptions = {
onLaunch (args) {
if (this.$vm) { // 已经初始化过了,主要是为了百度,百度 onShow 在 onLaunch 之前
return
}
this.$vm = vm
this.$vm.$mp = {
app: this
}
this.$vm.$scope = this
// vm 上也挂载 globalData
this.$vm.globalData = this.globalData
this.$vm._isMounted = true
this.$vm.__call_hook('mounted', args)
this.$vm.__call_hook('onLaunch', args)
}
}
// 兼容旧版本 globalData
appOptions.globalData = vm.$options.globalData || {}
// 将 methods 中的方法挂在 getApp() 中
const methods = vm.$options.methods
if (methods) {
Object.keys(methods).forEach(name => {
appOptions[name] = methods[name]
})
}
initHooks(appOptions, hooks)
return appOptions
}
import parseBaseApp from './app-base-parser' import parseBaseApp from '../../../mp-weixin/runtime/wrapper/app-parser'
import {
mocks,
initRefs
} from './util'
export default function parseApp (vm) { export default function parseApp (vm) {
return parseBaseApp(vm, { return parseBaseApp(vm)
mocks,
initRefs
})
} }
import Vue from 'vue'
import {
initData,
initSlots,
initVueIds,
handleEvent,
initBehaviors,
initProperties,
initVueComponent
} from 'uni-wrapper/util'
import {
handleLink,
initBehavior
} from './util'
export default function parseBaseComponent (vueComponentOptions, {
isPage,
initRelation
} = {}) {
const [VueComponent, vueOptions] = initVueComponent(Vue, vueComponentOptions)
const options = {
multipleSlots: true,
addGlobalClass: true
}
const componentOptions = {
options,
data: initData(vueOptions, Vue.prototype),
behaviors: initBehaviors(vueOptions, initBehavior),
properties: initProperties(vueOptions.props, false, vueOptions.__file),
lifetimes: {
attached () {
const properties = this.properties
const options = {
mpType: isPage.call(this) ? 'page' : 'component',
mpInstance: this,
propsData: properties
}
initVueIds(properties.vueId, this)
// 处理父子关系
initRelation.call(this, {
vuePid: this._$vuePid,
vueOptions: options
})
// 初始化 vue 实例
this.$vm = new VueComponent(options)
// 处理$slots,$scopedSlots(暂不支持动态变化$slots)
initSlots(this.$vm, properties.vueSlots)
// 触发首次 setData
this.$vm.$mount()
},
ready () {
// 当组件 props 默认值为 true,初始化时传入 false 会导致 created,ready 触发, 但 attached 不触发
// https://developers.weixin.qq.com/community/develop/doc/00066ae2844cc0f8eb883e2a557800
if (this.$vm) {
this.$vm._isMounted = true
this.$vm.__call_hook('mounted')
this.$vm.__call_hook('onReady')
} else {
// this.is && console.warn(this.is + ' is not attached')
}
},
detached () {
this.$vm.$destroy()
}
},
pageLifetimes: {
show (args) {
this.$vm && this.$vm.__call_hook('onPageShow', args)
},
hide () {
this.$vm && this.$vm.__call_hook('onPageHide')
},
resize (size) {
this.$vm && this.$vm.__call_hook('onPageResize', size)
}
},
methods: {
__l: handleLink,
__e: handleEvent
}
}
if (Array.isArray(vueOptions.wxsCallMethods)) {
vueOptions.wxsCallMethods.forEach(callMethod => {
componentOptions.methods[callMethod] = function (args) {
return this.$vm[callMethod](args)
}
})
}
if (isPage) {
return componentOptions
}
return [componentOptions, VueComponent]
}
import parseBaseComponent from './component-base-parser' import parseBaseComponent from '../../../mp-weixin/runtime/wrapper/component-parser'
import {
isPage,
initRelation
} from './util'
export default function parseComponent (vueComponentOptions) { export default function parseComponent (vueComponentOptions) {
return parseBaseComponent(vueComponentOptions, { return parseBaseComponent(vueComponentOptions)
isPage,
initRelation
})
} }
import {
initHooks,
PAGE_EVENT_HOOKS
} from 'uni-wrapper/util'
import parseComponent from 'uni-platform/runtime/wrapper/component-parser'
const hooks = [
'onShow',
'onHide',
'onUnload'
]
hooks.push(...PAGE_EVENT_HOOKS)
export default function parseBasePage (vuePageOptions, {
isPage,
initRelation
}) {
const pageOptions = parseComponent(vuePageOptions, {
isPage,
initRelation
})
initHooks(pageOptions.methods, hooks, vuePageOptions)
pageOptions.methods.onLoad = function (args) {
this.$vm.$mp.query = args // 兼容 mpvue
this.$vm.__call_hook('onLoad', args)
}
return pageOptions
}
import parseBasePage from './page-base-parser' import parseBasePage from '../../../mp-weixin/runtime/wrapper/page-parser'
import {
isPage,
initRelation
} from './util'
export default function parsePage (vuePageOptions) { export default function parsePage (vuePageOptions) {
return parseBasePage(vuePageOptions, { return parseBasePage(vuePageOptions)
isPage,
initRelation
})
} }
/* 快手也使用__wxExparserNodeId__和__wxWebviewId__ */
export const mocks = ['__route__', '__wxExparserNodeId__', '__wxWebviewId__']
export function findVmByVueId (vm, vuePid) {
const $children = vm.$children
// 优先查找直属
let parentVm = $children.find(childVm => childVm.$scope._$vueId === vuePid)
if (parentVm) {
return parentVm
}
// 反向递归查找
for (let i = $children.length - 1; i >= 0; i--) {
parentVm = findVmByVueId($children[i], vuePid)
if (parentVm) {
return parentVm
}
}
}
export function initBehavior (options) {
return Behavior(options)
}
export function isPage () {
return !!this.route
}
export function initRelation (detail) {
this.triggerEvent('__l', detail)
}
export function initRefs (vm) {
const mpInstance = vm.$scope
Object.defineProperty(vm, '$refs', {
get () {
const $refs = {}
const components = mpInstance.selectAllComponents('.vue-ref')
components.forEach(component => {
const ref = component.dataset.ref
$refs[ref] = component.$vm || component
})
const forComponents = mpInstance.selectAllComponents('.vue-ref-in-for')
forComponents.forEach(component => {
const ref = component.dataset.ref
if (!$refs[ref]) {
$refs[ref] = []
}
$refs[ref].push(component.$vm || component)
})
return $refs
}
})
}
export function handleLink (event) {
const {
vuePid,
vueOptions
} = event.detail || event.value // detail 是微信,value 是百度(dipatch)
let parentVm
if (vuePid) {
parentVm = findVmByVueId(this.$vm, vuePid)
}
if (!parentVm) {
parentVm = this.$vm
}
vueOptions.parent = parentVm
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册