提交 41bdae3f 编写于 作者: fxy060608's avatar fxy060608

feat(runtime): mp-weixin $parent ,$children

上级 6d6bdf84
......@@ -432,7 +432,21 @@ function createObserver (name) {
}
function getProperties (props) {
const properties = {};
const properties = {
vueSlots: { // 小程序不能直接定义 $slots 的 props,所以通过 vueSlots 转换到 $slots
type: null,
value: [],
observer: function (newVal, oldVal) {
const $slots = Object.create(null);
newVal.forEach(slotName => {
$slots[slotName] = true;
});
this.setData({
$slots
});
}
}
};
if (Array.isArray(props)) { // ['title']
props.forEach(key => {
properties[key] = {
......@@ -540,10 +554,6 @@ function handleEvent (event) {
});
}
function handleLink (event) {
event.detail.$parent = this.$vm;
}
function initRefs (vm) {
const mpInstance = vm.$mp[vm.mpType];
Object.defineProperty(vm, '$refs', {
......@@ -567,20 +577,6 @@ function initRefs (vm) {
});
}
function initChildren (vm) {
const mpInstance = vm.$mp[vm.mpType];
Object.defineProperty(vm, '$children', {
get () {
const $children = [];
const components = mpInstance.selectAllComponents('.vue-com');
components.forEach(component => {
$children.push(component.$vm);
});
return $children
}
});
}
function baiduComponentDestroy ($vm) {
$vm.$children.forEach(childVm => {
childVm.$mp.component.detached();
......@@ -621,7 +617,6 @@ function createApp (vueOptions) {
if (this.mpType !== 'app') {
initRefs(this);
initMocks(this);
initChildren(this);
}
}
});
......@@ -645,6 +640,35 @@ function createApp (vueOptions) {
return vueOptions
}
function triggerLink (mpInstance) {
const baiduComponentInstances = mpInstance.pageinstance.$baiduComponentInstances;
baiduComponentInstances[mpInstance.id] = mpInstance;
if (mpInstance.ownerId) { // 组件嵌组件
const parentBaiduComponentInstance = baiduComponentInstances[mpInstance.ownerId];
if (parentBaiduComponentInstance) {
handleLink.call(parentBaiduComponentInstance, {
detail: mpInstance
});
} else {
console.error(`查找父组件失败${mpInstance.ownerId}`);
}
} else { // 页面直属组件
handleLink.call(mpInstance.pageinstance, {
detail: mpInstance
});
}
}
function handleLink (event) {
if (!event.detail.$parent) {
event.detail.$parent = this.$vm;
event.detail.$parent.$children.push(event.detail);
event.detail.$root = this.$vm.$root;
}
}
const hooks$1 = [
'onShow',
'onHide',
......@@ -679,7 +703,7 @@ function createPage (vueOptions) {
this.$vm.__call_hook('onLoad', args);
},
onReady () {
this.$vm._isMounted = true;
this.$vm._isMounted = true;
this.$vm.__call_hook('mounted');
this.$vm.__call_hook('onReady');
},
......@@ -698,7 +722,7 @@ function createPage (vueOptions) {
return Page(pageOptions)
}
function initVueComponent (mpInstace, VueComponent) {
function initVueComponent (mpInstace, VueComponent) {
if (mpInstace.$vm) {
return
}
......@@ -711,6 +735,16 @@ function initVueComponent (mpInstace, VueComponent) {
// 初始化 vue 实例
mpInstace.$vm = new VueComponent(options);
// 处理$slots,$scopedSlots(暂不支持动态变化$slots)
const vueSlots = mpInstace.properties.vueSlots;
if (Array.isArray(vueSlots) && vueSlots.length) {
const $slots = Object.create(null);
vueSlots.forEach(slotName => {
$slots[slotName] = true;
});
mpInstace.$vm.$scopedSlots = mpInstace.$vm.$slots = $slots;
}
// 初始化渲染数据
mpInstace.$vm.$mount();
}
......@@ -736,21 +770,7 @@ function createComponent (vueOptions) {
ready () {
initVueComponent(this, VueComponent); // 目前发现部分情况小程序 attached 不触发
{
const baiduComponentInstances = this.pageinstance.$baiduComponentInstances;
baiduComponentInstances[this.id] = this;
if (this.ownerId) { // 组件嵌组件
const parentBaiduComponentInstance = baiduComponentInstances[this.ownerId];
if (parentBaiduComponentInstance) {
this.$vm.$parent = parentBaiduComponentInstance.$vm;
} else {
console.error(`查找父组件失败${this.ownerId}`);
}
} else { // 页面直属组件
this.$vm.$parent = this.pageinstance.$vm;
}
}
triggerLink(this);
const eventId = this.dataset.eventId;
if (eventId) {
......
{
"name": "@dcloudio/uni-mp-baidu",
"version": "0.0.806",
"version": "0.0.809",
"description": "uni-app mp-baidu",
"main": "dist/index.js",
"scripts": {
......
......@@ -477,7 +477,21 @@ function createObserver (name) {
}
function getProperties (props) {
const properties = {};
const properties = {
vueSlots: { // 小程序不能直接定义 $slots 的 props,所以通过 vueSlots 转换到 $slots
type: null,
value: [],
observer: function (newVal, oldVal) {
const $slots = Object.create(null);
newVal.forEach(slotName => {
$slots[slotName] = true;
});
this.setData({
$slots
});
}
}
};
if (Array.isArray(props)) { // ['title']
props.forEach(key => {
properties[key] = {
......@@ -579,10 +593,6 @@ function handleEvent (event) {
});
}
function handleLink (event) {
event.detail.$parent = this.$vm;
}
function initRefs (vm) {
const mpInstance = vm.$mp[vm.mpType];
Object.defineProperty(vm, '$refs', {
......@@ -604,20 +614,6 @@ function initRefs (vm) {
return $refs
}
});
}
function initChildren (vm) {
const mpInstance = vm.$mp[vm.mpType];
Object.defineProperty(vm, '$children', {
get () {
const $children = [];
const components = mpInstance.selectAllComponents('.vue-com');
components.forEach(component => {
$children.push(component.$vm);
});
return $children
}
});
}
const hooks = [
......@@ -646,7 +642,6 @@ function createApp (vueOptions) {
if (this.mpType !== 'app') {
initRefs(this);
initMocks(this);
initChildren(this);
}
}
});
......@@ -670,6 +665,37 @@ function createApp (vueOptions) {
return vueOptions
}
const instances = Object.create(null);
function triggerLink (mpInstance) {
const nodeId = mpInstance.__nodeid__ + '';
const webviewId = mpInstance.__webviewId__ + '';
instances[webviewId + '_' + nodeId] = mpInstance.$vm;
mpInstance.triggerEvent('__l', {
nodeId,
webviewId
}, {
bubbles: true,
composed: true
});
}
// TODO 目前有 bug,composed 不生效
function handleLink (event) {
const nodeId = event.detail.nodeId;
const webviewId = event.detail.webviewId;
const childVm = instances[webviewId + '_' + nodeId];
if (childVm) {
childVm.$parent = this.$vm;
childVm.$parent.$children.push(event.detail);
childVm.$root = this.$vm.$root;
delete instances[webviewId + '_' + nodeId];
}
}
const hooks$1 = [
'onShow',
'onHide',
......@@ -701,7 +727,7 @@ function createPage (vueOptions) {
this.$vm.__call_hook('onLoad', args);
},
onReady () {
this.$vm._isMounted = true;
this.$vm._isMounted = true;
this.$vm.__call_hook('mounted');
this.$vm.__call_hook('onReady');
},
......@@ -720,7 +746,7 @@ function createPage (vueOptions) {
return Page(pageOptions)
}
function initVueComponent (mpInstace, VueComponent) {
function initVueComponent (mpInstace, VueComponent) {
if (mpInstace.$vm) {
return
}
......@@ -733,6 +759,16 @@ function initVueComponent (mpInstace, VueComponent) {
// 初始化 vue 实例
mpInstace.$vm = new VueComponent(options);
// 处理$slots,$scopedSlots(暂不支持动态变化$slots)
const vueSlots = mpInstace.properties.vueSlots;
if (Array.isArray(vueSlots) && vueSlots.length) {
const $slots = Object.create(null);
vueSlots.forEach(slotName => {
$slots[slotName] = true;
});
mpInstace.$vm.$scopedSlots = mpInstace.$vm.$slots = $slots;
}
// 初始化渲染数据
mpInstace.$vm.$mount();
}
......@@ -758,9 +794,7 @@ function createComponent (vueOptions) {
ready () {
initVueComponent(this, VueComponent); // 目前发现部分情况小程序 attached 不触发
{
this.triggerEvent('__l', this.$vm); // TODO 百度仅能传递 json 对象
}
triggerLink(this);
const eventId = this.dataset.eventId;
if (eventId) {
......
{
"name": "@dcloudio/uni-mp-toutiao",
"version": "0.0.303",
"version": "0.0.305",
"description": "uni-app mp-toutiao",
"main": "dist/index.js",
"scripts": {
......
......@@ -319,7 +319,21 @@ function createObserver (name) {
}
function getProperties (props) {
const properties = {};
const properties = {
vueSlots: { // 小程序不能直接定义 $slots 的 props,所以通过 vueSlots 转换到 $slots
type: null,
value: [],
observer: function (newVal, oldVal) {
const $slots = Object.create(null);
newVal.forEach(slotName => {
$slots[slotName] = true;
});
this.setData({
$slots
});
}
}
};
if (Array.isArray(props)) { // ['title']
props.forEach(key => {
properties[key] = {
......@@ -421,10 +435,6 @@ function handleEvent (event) {
});
}
function handleLink (event) {
event.detail.$parent = this.$vm;
}
function initRefs (vm) {
const mpInstance = vm.$mp[vm.mpType];
Object.defineProperty(vm, '$refs', {
......@@ -446,20 +456,6 @@ function initRefs (vm) {
return $refs
}
});
}
function initChildren (vm) {
const mpInstance = vm.$mp[vm.mpType];
Object.defineProperty(vm, '$children', {
get () {
const $children = [];
const components = mpInstance.selectAllComponents('.vue-com');
components.forEach(component => {
$children.push(component.$vm);
});
return $children
}
});
}
const hooks = [
......@@ -488,7 +484,6 @@ function createApp (vueOptions) {
if (this.mpType !== 'app') {
initRefs(this);
initMocks(this);
initChildren(this);
}
}
});
......@@ -512,6 +507,22 @@ function createApp (vueOptions) {
return vueOptions
}
function triggerLink (mpInstance) {
mpInstance.triggerEvent('__l', mpInstance.$vm, {
bubbles: true,
composed: true
});
}
function handleLink (event) {
if (!event.detail.$parent) {
event.detail.$parent = this.$vm;
event.detail.$parent.$children.push(event.detail);
event.detail.$root = this.$vm.$root;
}
}
const hooks$1 = [
'onShow',
'onHide',
......@@ -543,7 +554,7 @@ function createPage (vueOptions) {
this.$vm.__call_hook('onLoad', args);
},
onReady () {
this.$vm._isMounted = true;
this.$vm._isMounted = true;
this.$vm.__call_hook('mounted');
this.$vm.__call_hook('onReady');
},
......@@ -562,7 +573,7 @@ function createPage (vueOptions) {
return Page(pageOptions)
}
function initVueComponent (mpInstace, VueComponent) {
function initVueComponent (mpInstace, VueComponent) {
if (mpInstace.$vm) {
return
}
......@@ -575,6 +586,16 @@ function initVueComponent (mpInstace, VueComponent) {
// 初始化 vue 实例
mpInstace.$vm = new VueComponent(options);
// 处理$slots,$scopedSlots(暂不支持动态变化$slots)
const vueSlots = mpInstace.properties.vueSlots;
if (Array.isArray(vueSlots) && vueSlots.length) {
const $slots = Object.create(null);
vueSlots.forEach(slotName => {
$slots[slotName] = true;
});
mpInstace.$vm.$scopedSlots = mpInstace.$vm.$slots = $slots;
}
// 初始化渲染数据
mpInstace.$vm.$mount();
}
......@@ -600,9 +621,7 @@ function createComponent (vueOptions) {
ready () {
initVueComponent(this, VueComponent); // 目前发现部分情况小程序 attached 不触发
{
this.triggerEvent('__l', this.$vm); // TODO 百度仅能传递 json 对象
}
triggerLink(this);
const eventId = this.dataset.eventId;
if (eventId) {
......
{
"name": "@dcloudio/uni-mp-weixin",
"version": "0.0.907",
"version": "0.0.909",
"description": "uni-app mp-weixin",
"main": "dist/index.js",
"scripts": {
......
......@@ -3,8 +3,7 @@ import Vue from 'vue'
import {
initRefs,
initHooks,
initMocks,
initChildren
initMocks
} from './util'
const hooks = [
......@@ -33,7 +32,6 @@ export function createApp (vueOptions) {
if (this.mpType !== 'app') {
initRefs(this)
initMocks(this)
initChildren(this)
}
}
})
......
import Vue from 'vue'
import {
getData,
handleLink,
triggerLink
} from 'uni-platform/runtime/wrapper/index'
import {
getData,
handleEvent,
getProperties
} from './util'
function initVueComponent (mpInstace, VueComponent) {
function initVueComponent (mpInstace, VueComponent) {
if (mpInstace.$vm) {
return
}
......@@ -20,6 +24,16 @@ function initVueComponent (mpInstace, VueComponent) {
// 初始化 vue 实例
mpInstace.$vm = new VueComponent(options)
// 处理$slots,$scopedSlots(暂不支持动态变化$slots)
const vueSlots = mpInstace.properties.vueSlots
if (Array.isArray(vueSlots) && vueSlots.length) {
const $slots = Object.create(null)
vueSlots.forEach(slotName => {
$slots[slotName] = true
})
mpInstace.$vm.$scopedSlots = mpInstace.$vm.$slots = $slots
}
// 初始化渲染数据
mpInstace.$vm.$mount()
}
......@@ -45,23 +59,7 @@ export function createComponent (vueOptions) {
ready () {
initVueComponent(this, VueComponent) // 目前发现部分情况小程序 attached 不触发
if (__PLATFORM__ === 'mp-baidu') {
const baiduComponentInstances = this.pageinstance.$baiduComponentInstances
baiduComponentInstances[this.id] = this
if (this.ownerId) { // 组件嵌组件
const parentBaiduComponentInstance = baiduComponentInstances[this.ownerId]
if (parentBaiduComponentInstance) {
this.$vm.$parent = parentBaiduComponentInstance.$vm
} else {
console.error(`查找父组件失败${this.ownerId}`)
}
} else { // 页面直属组件
this.$vm.$parent = this.pageinstance.$vm
}
} else {
this.triggerEvent('__l', this.$vm) // TODO 百度仅能传递 json 对象
}
triggerLink(this)
const eventId = this.dataset.eventId
if (eventId) {
......
import Vue from 'vue'
import {
handleLink
} from 'uni-platform/runtime/wrapper/index'
import {
getData,
initHooks,
handleLink,
handleEvent,
baiduPageDestroy
} from './util'
......@@ -42,7 +45,7 @@ export function createPage (vueOptions) {
this.$vm.__call_hook('onLoad', args)
},
onReady () {
this.$vm._isMounted = true
this.$vm._isMounted = true
this.$vm.__call_hook('mounted')
this.$vm.__call_hook('onReady')
},
......
......@@ -56,7 +56,21 @@ function createObserver (name) {
}
export function getProperties (props) {
const properties = {}
const properties = {
vueSlots: { // 小程序不能直接定义 $slots 的 props,所以通过 vueSlots 转换到 $slots
type: null,
value: [],
observer: function (newVal, oldVal) {
const $slots = Object.create(null)
newVal.forEach(slotName => {
$slots[slotName] = true
})
this.setData({
$slots
})
}
}
}
if (Array.isArray(props)) { // ['title']
props.forEach(key => {
properties[key] = {
......@@ -164,10 +178,6 @@ export function handleEvent (event) {
})
}
export function handleLink (event) {
event.detail.$parent = this.$vm
}
export function initRefs (vm) {
const mpInstance = vm.$mp[vm.mpType]
Object.defineProperty(vm, '$refs', {
......@@ -191,20 +201,6 @@ export function initRefs (vm) {
})
}
export function initChildren (vm) {
const mpInstance = vm.$mp[vm.mpType]
Object.defineProperty(vm, '$children', {
get () {
const $children = []
const components = mpInstance.selectAllComponents('.vue-com')
components.forEach(component => {
$children.push(component.$vm)
})
return $children
}
})
}
function baiduComponentDestroy ($vm) {
$vm.$children.forEach(childVm => {
childVm.$mp.component.detached()
......
export function triggerLink (mpInstance) {
const baiduComponentInstances = mpInstance.pageinstance.$baiduComponentInstances
baiduComponentInstances[mpInstance.id] = mpInstance
if (mpInstance.ownerId) { // 组件嵌组件
const parentBaiduComponentInstance = baiduComponentInstances[mpInstance.ownerId]
if (parentBaiduComponentInstance) {
handleLink.call(parentBaiduComponentInstance, {
detail: mpInstance
})
} else {
console.error(`查找父组件失败${mpInstance.ownerId}`)
}
} else { // 页面直属组件
handleLink.call(mpInstance.pageinstance, {
detail: mpInstance
})
}
}
export function handleLink (event) {
if (!event.detail.$parent) {
event.detail.$parent = this.$vm
event.detail.$parent.$children.push(event.detail)
event.detail.$root = this.$vm.$root
}
}
const instances = Object.create(null)
export function triggerLink (mpInstance) {
const nodeId = mpInstance.__nodeid__ + ''
const webviewId = mpInstance.__webviewId__ + ''
instances[webviewId + '_' + nodeId] = mpInstance.$vm
mpInstance.triggerEvent('__l', {
nodeId,
webviewId
}, {
bubbles: true,
composed: true
})
}
// TODO 目前有 bug,composed 不生效
export function handleLink (event) {
const nodeId = event.detail.nodeId
const webviewId = event.detail.webviewId
const childVm = instances[webviewId + '_' + nodeId]
if (childVm) {
childVm.$parent = this.$vm
childVm.$parent.$children.push(event.detail)
childVm.$root = this.$vm.$root
delete instances[webviewId + '_' + nodeId]
}
}
export function triggerLink (mpInstance) {
mpInstance.triggerEvent('__l', mpInstance.$vm, {
bubbles: true,
composed: true
})
}
export function handleLink (event) {
if (!event.detail.$parent) {
event.detail.$parent = this.$vm
event.detail.$parent.$children.push(event.detail)
event.detail.$root = this.$vm.$root
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册