提交 3d313d67 编写于 作者: fxy060608's avatar fxy060608

feat: globalData in setup (#3159)

上级 02e1d620
......@@ -2408,6 +2408,18 @@ var serviceContext = (function (vue) {
extraData: {},
},
};
}
function defineGlobalData(app, defaultGlobalData) {
const options = app.$options || {};
options.globalData = extend(options.globalData || {}, defaultGlobalData);
Object.defineProperty(app, 'globalData', {
get() {
return options.globalData;
},
set(newGlobalData) {
options.globalData = newGlobalData;
},
});
}
function getRealPath(filepath) {
......@@ -14865,8 +14877,7 @@ var serviceContext = (function (vue) {
}
const request = defineTaskApi(API_REQUEST, (args, { resolve, reject }) => {
let { header, method, data, timeout, url, responseType, sslVerify, firstIpv4,
// NOTE 属性有但是types没有
// @ts-ignore
// @ts-ignore tls 缺少 types 类型
tls, } = args;
let contentType;
for (const name in header) {
......@@ -19107,10 +19118,7 @@ var serviceContext = (function (vue) {
appCtx = appVm;
initAppVm(appCtx);
extend(appCtx, defaultApp); // 拷贝默认实现
const { $options } = appVm;
if ($options) {
appCtx.globalData = extend($options.globalData || {}, appCtx.globalData);
}
defineGlobalData(appCtx, defaultApp.globalData);
initService();
initEntry();
initTabBar();
......
import { ComponentPublicInstance } from 'vue'
import { extend } from '@vue/shared'
import { formatLog } from '@dcloudio/uni-shared'
import { initAppVm, initService } from '@dcloudio/uni-core'
import { initAppVm, initService, defineGlobalData } from '@dcloudio/uni-core'
import { initEntry } from './initEntry'
import { initTabBar } from './initTabBar'
......@@ -42,10 +42,7 @@ export function registerApp(appVm: ComponentPublicInstance) {
extend(appCtx, defaultApp) // 拷贝默认实现
const { $options } = appVm
if ($options) {
appCtx.globalData = extend($options.globalData || {}, appCtx.globalData)
}
defineGlobalData(appCtx, defaultApp.globalData)
initService()
......
import type { ComponentPublicInstance } from '@vue/runtime-core'
import { extend } from '@vue/shared'
export interface LaunchOptions {
path: string
query: Record<string, any>
......@@ -16,3 +19,19 @@ export function createLaunchOptions() {
},
}
}
export function defineGlobalData(
app: ComponentPublicInstance,
defaultGlobalData?: Record<string, unknown>
) {
const options = app.$options || {}
options.globalData = extend(options.globalData || {}, defaultGlobalData)
Object.defineProperty(app, 'globalData', {
get() {
return options.globalData
},
set(newGlobalData) {
options.globalData = newGlobalData
},
})
}
......@@ -480,6 +480,18 @@ function initPageVm(pageVm, page) {
pageVm.$.__isActive = true;
}
}
function defineGlobalData(app, defaultGlobalData) {
const options = app.$options || {};
options.globalData = shared.extend(options.globalData || {}, defaultGlobalData);
Object.defineProperty(app, "globalData", {
get() {
return options.globalData;
},
set(newGlobalData) {
options.globalData = newGlobalData;
}
});
}
function converPx(value) {
if (/^-?\d+[ur]px$/i.test(value)) {
return value.replace(/(^-?\d+)[ur]px$/i, (text, num) => {
......@@ -4265,6 +4277,7 @@ const props$k = {
};
var index$t = /* @__PURE__ */ defineBuiltInComponent({
name: "Navigator",
inheritAttrs: false,
compatConfig: {
MODE: 3
},
......@@ -4272,6 +4285,15 @@ var index$t = /* @__PURE__ */ defineBuiltInComponent({
setup(props2, {
slots
}) {
const vm = vue.getCurrentInstance();
const __scopeId = vm && vm.root.type.__scopeId || "";
const {
$attrs,
$excludeAttrs,
$listeners
} = useAttrs({
excludeListeners: true
});
const {
hovering,
binding
......@@ -4312,14 +4334,21 @@ var index$t = /* @__PURE__ */ defineBuiltInComponent({
}
return () => {
const {
hoverClass
hoverClass,
url
} = props2;
const hasHoverClass = props2.hoverClass && props2.hoverClass !== "none";
return vue.createVNode("uni-navigator", vue.mergeProps({
return vue.createVNode("a", {
"class": "navigator-wrap",
"href": url,
"onClick": onEventPrevent
}, [vue.createVNode("uni-navigator", vue.mergeProps({
"class": hasHoverClass && hovering.value ? hoverClass : ""
}, hasHoverClass && binding, {
}, hasHoverClass && binding, $attrs.value, $excludeAttrs.value, $listeners.value, {
[__scopeId]: ""
}, {
"onClick": onClick
}), [slots.default && slots.default()], 16, ["onClick"]);
}), [slots.default && slots.default()], 16, ["onClick"])], 8, ["href", "onClick"]);
};
}
});
......@@ -5130,11 +5159,10 @@ var index$p = /* @__PURE__ */ defineBuiltInComponent({
trigger("itemclick", e2, detail);
}
function _renderNodes(nodes) {
var _a;
if (typeof nodes === "string") {
nodes = parseHtml(nodes);
}
const nodeList = parseNodes(nodes, document.createDocumentFragment(), ((_a = vm == null ? void 0 : vm.root) == null ? void 0 : _a.type).__scopeId || "", hasItemClick && triggerItemClick);
const nodeList = parseNodes(nodes, document.createDocumentFragment(), (vm && vm.root.type).__scopeId || "", hasItemClick && triggerItemClick);
rootRef.value.firstElementChild.innerHTML = "";
rootRef.value.firstElementChild.appendChild(nodeList);
}
......@@ -6909,7 +6937,7 @@ function getApp$1() {
function initApp(vm) {
appVm = vm;
initAppVm(appVm);
appVm.globalData = appVm.$options.globalData || {};
defineGlobalData(appVm);
}
function wrapperComponentSetup(comp, { clone, init, setup, before }) {
if (clone) {
......@@ -6976,8 +7004,12 @@ function setupApp(comp) {
},
before(comp2) {
comp2.mpType = "app";
comp2.setup = () => () => {
return vue.openBlock(), vue.createBlock(LayoutComponent);
const { setup } = comp2;
comp2.setup = (props2, ctx) => {
setup && setup(props2, ctx);
return () => {
return vue.openBlock(), vue.createBlock(LayoutComponent);
};
};
}
});
......
......@@ -1512,6 +1512,18 @@ function createLaunchOptions() {
}
};
}
function defineGlobalData(app, defaultGlobalData) {
const options = app.$options || {};
options.globalData = extend(options.globalData || {}, defaultGlobalData);
Object.defineProperty(app, "globalData", {
get() {
return options.globalData;
},
set(newGlobalData) {
options.globalData = newGlobalData;
}
});
}
function converPx(value) {
if (/^-?\d+[ur]px$/i.test(value)) {
return value.replace(/(^-?\d+)[ur]px$/i, (text2, num) => {
......@@ -9997,6 +10009,7 @@ const props$r = {
};
var index$q = /* @__PURE__ */ defineBuiltInComponent({
name: "Navigator",
inheritAttrs: false,
compatConfig: {
MODE: 3
},
......@@ -10004,6 +10017,15 @@ var index$q = /* @__PURE__ */ defineBuiltInComponent({
setup(props2, {
slots
}) {
const vm = getCurrentInstance();
const __scopeId = vm && vm.root.type.__scopeId || "";
const {
$attrs,
$excludeAttrs,
$listeners
} = useAttrs({
excludeListeners: true
});
const {
hovering,
binding
......@@ -10044,14 +10066,21 @@ var index$q = /* @__PURE__ */ defineBuiltInComponent({
}
return () => {
const {
hoverClass
hoverClass,
url
} = props2;
const hasHoverClass = props2.hoverClass && props2.hoverClass !== "none";
return createVNode("uni-navigator", mergeProps({
return createVNode("a", {
"class": "navigator-wrap",
"href": url,
"onClick": onEventPrevent
}, [createVNode("uni-navigator", mergeProps({
"class": hasHoverClass && hovering.value ? hoverClass : ""
}, hasHoverClass && binding, {
}, hasHoverClass && binding, $attrs.value, $excludeAttrs.value, $listeners.value, {
[__scopeId]: ""
}, {
"onClick": onClick
}), [slots.default && slots.default()], 16, ["onClick"]);
}), [slots.default && slots.default()], 16, ["onClick"])], 8, ["href", "onClick"]);
};
}
});
......@@ -11652,11 +11681,10 @@ var index$m = /* @__PURE__ */ defineBuiltInComponent({
trigger("itemclick", e2, detail);
}
function _renderNodes(nodes) {
var _a;
if (typeof nodes === "string") {
nodes = parseHtml(nodes);
}
const nodeList = parseNodes(nodes, document.createDocumentFragment(), ((_a = vm == null ? void 0 : vm.root) == null ? void 0 : _a.type).__scopeId || "", hasItemClick && triggerItemClick);
const nodeList = parseNodes(nodes, document.createDocumentFragment(), (vm && vm.root.type).__scopeId || "", hasItemClick && triggerItemClick);
rootRef.value.firstElementChild.innerHTML = "";
rootRef.value.firstElementChild.appendChild(nodeList);
}
......@@ -13964,7 +13992,7 @@ function getApp$1() {
function initApp(vm) {
appVm = vm;
initAppVm(appVm);
appVm.globalData = appVm.$options.globalData || {};
defineGlobalData(appVm);
initService();
initView();
}
......@@ -14092,8 +14120,12 @@ function setupApp(comp) {
},
before(comp2) {
comp2.mpType = "app";
comp2.setup = () => () => {
return openBlock(), createBlock(LayoutComponent);
const { setup } = comp2;
comp2.setup = (props2, ctx) => {
setup && setup(props2, ctx);
return () => {
return openBlock(), createBlock(LayoutComponent);
};
};
}
});
......
import { ComponentPublicInstance } from 'vue'
import { initAppVm, initService, initView } from '@dcloudio/uni-core'
import {
initAppVm,
initService,
initView,
defineGlobalData,
} from '@dcloudio/uni-core'
let appVm: ComponentPublicInstance
......@@ -10,7 +15,7 @@ export function getApp() {
export function initApp(vm: ComponentPublicInstance) {
appVm = vm
initAppVm(appVm)
appVm.globalData = appVm.$options.globalData || {}
defineGlobalData(appVm)
initService()
initView()
}
......@@ -183,8 +183,12 @@ export function setupApp(comp: any) {
},
before(comp) {
comp.mpType = 'app'
comp.setup = () => () => {
return openBlock(), createBlock(LayoutComponent)
const { setup } = comp
comp.setup = (props, ctx) => {
setup && setup(props, ctx)
return () => {
return openBlock(), createBlock(LayoutComponent)
}
}
},
})
......
......@@ -56,15 +56,12 @@ export function parseApp(
// 已经初始化过了,主要是为了百度,百度 onShow 在 onLaunch 之前
return
}
initBaseInstance(internalInstance, {
mpType: 'app',
mpInstance: this,
slots: [],
})
injectAppLaunchHooks(internalInstance)
ctx.globalData = this.globalData
instance.$callHook(
ON_LAUNCH,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册