提交 77951dd9 编写于 作者: fxy060608's avatar fxy060608

wip(app): nvue

上级 ac3a53cf
...@@ -18148,7 +18148,7 @@ function preloadWebview({ url, path, query, }) { ...@@ -18148,7 +18148,7 @@ function preloadWebview({ url, path, query, }) {
return preloadWebviews[url]; return preloadWebviews[url];
} }
function registerPage({ url, path, query, openType, webview, eventChannel, }) { function registerPage({ url, path, query, openType, webview, nvuePageVm, eventChannel, }) {
// fast 模式,nvue 首页时,会在nvue中主动调用registerPage并传入首页webview,此时初始化一下首页(因为此时可能还未调用registerApp) // fast 模式,nvue 首页时,会在nvue中主动调用registerPage并传入首页webview,此时初始化一下首页(因为此时可能还未调用registerApp)
if (webview) { if (webview) {
initEntry(); initEntry();
...@@ -18196,12 +18196,17 @@ function registerPage({ url, path, query, openType, webview, eventChannel, }) { ...@@ -18196,12 +18196,17 @@ function registerPage({ url, path, query, openType, webview, eventChannel, }) {
const route = path.substr(1); const route = path.substr(1);
webview.__uniapp_route = route; webview.__uniapp_route = route;
const pageInstance = initPageInternalInstance(openType, url, query, routeOptions.meta, eventChannel); const pageInstance = initPageInternalInstance(openType, url, query, routeOptions.meta, eventChannel);
initNVueEntryPage(webview); const id = parseInt(webview.id);
if (webview.nvue) { if (webview.nvue) {
createNVuePage(parseInt(webview.id), webview, pageInstance); if (id === 1 && nvuePageVm) {
initNVueEntryPage(webview, nvuePageVm, pageInstance);
}
else {
createNVuePage(id, webview, pageInstance);
}
} }
else { else {
createVuePage(parseInt(webview.id), route, query, pageInstance, initPageOptions(routeOptions)); createVuePage(id, route, query, pageInstance, initPageOptions(routeOptions));
} }
return webview; return webview;
} }
...@@ -18227,19 +18232,18 @@ function initPageOptions({ meta }) { ...@@ -18227,19 +18232,18 @@ function initPageOptions({ meta }) {
windowBottom: tabBarInstance.indexOf(meta.route) >= 0 && tabBarInstance.cover ? tabBarInstance.height : 0, windowBottom: tabBarInstance.indexOf(meta.route) >= 0 && tabBarInstance.cover ? tabBarInstance.height : 0,
}; };
} }
function initNVueEntryPage(webview) { function initNVueEntryPage(webview, nvuePageVm, pageInstance) {
const isLaunchNVuePage = webview.id === '1' && webview.nvue; initPageVm(nvuePageVm, pageInstance);
addCurrentPage(nvuePageVm);
// 首页是 nvue 时,在 registerPage 时,执行路由堆栈 // 首页是 nvue 时,在 registerPage 时,执行路由堆栈
if (isLaunchNVuePage) { if (__uniConfig.splashscreen &&
if (__uniConfig.splashscreen && __uniConfig.splashscreen.autoclose &&
__uniConfig.splashscreen.autoclose && !__uniConfig.splashscreen.alwaysShowBeforeRender) {
!__uniConfig.splashscreen.alwaysShowBeforeRender) { plus.navigator.closeSplashscreen();
plus.navigator.closeSplashscreen();
}
__uniConfig.onReady(function () {
navigateFinish();
});
} }
__uniConfig.onReady(function () {
navigateFinish();
});
} }
function createNVuePage(pageId, webview, pageInstance) { function createNVuePage(pageId, webview, pageInstance) {
const fakeNVueVm = { const fakeNVueVm = {
...@@ -19140,6 +19144,16 @@ function registerApp(appVm) { ...@@ -19140,6 +19144,16 @@ function registerApp(appVm) {
if ((process.env.NODE_ENV !== 'production')) { if ((process.env.NODE_ENV !== 'production')) {
console.log(formatLog('registerApp')); console.log(formatLog('registerApp'));
} }
// 定制 useStore (主要是为了 nvue 共享)
if (uni.Vuex && appVm.$store) {
const { useStore } = uni.Vuex;
uni.Vuex.useStore = (key) => {
if (!key) {
return appVm.$store;
}
return useStore(key);
};
}
initVueApp(appVm); initVueApp(appVm);
appCtx = appVm; appCtx = appVm;
initAppVm(appCtx); initAppVm(appCtx);
......
...@@ -35,6 +35,17 @@ export function registerApp(appVm: ComponentPublicInstance) { ...@@ -35,6 +35,17 @@ export function registerApp(appVm: ComponentPublicInstance) {
console.log(formatLog('registerApp')) console.log(formatLog('registerApp'))
} }
// 定制 useStore (主要是为了 nvue 共享)
if ((uni as any).Vuex && (appVm as any).$store) {
const { useStore } = (uni as any).Vuex
;(uni as any).Vuex.useStore = (key: string) => {
if (!key) {
return (appVm as any).$store
}
return useStore(key)
}
}
initVueApp(appVm) initVueApp(appVm)
appCtx = appVm appCtx = appVm
......
...@@ -26,6 +26,7 @@ interface RegisterPageOptions { ...@@ -26,6 +26,7 @@ interface RegisterPageOptions {
query: Record<string, string> query: Record<string, string>
openType: UniApp.OpenType openType: UniApp.OpenType
webview?: PlusWebviewWebviewObject webview?: PlusWebviewWebviewObject
nvuePageVm?: ComponentPublicInstance
eventChannel?: EventChannel eventChannel?: EventChannel
} }
...@@ -35,6 +36,7 @@ export function registerPage({ ...@@ -35,6 +36,7 @@ export function registerPage({
query, query,
openType, openType,
webview, webview,
nvuePageVm,
eventChannel, eventChannel,
}: RegisterPageOptions) { }: RegisterPageOptions) {
// fast 模式,nvue 首页时,会在nvue中主动调用registerPage并传入首页webview,此时初始化一下首页(因为此时可能还未调用registerApp) // fast 模式,nvue 首页时,会在nvue中主动调用registerPage并传入首页webview,此时初始化一下首页(因为此时可能还未调用registerApp)
...@@ -105,18 +107,16 @@ export function registerPage({ ...@@ -105,18 +107,16 @@ export function registerPage({
eventChannel eventChannel
) )
initNVueEntryPage(webview) const id = parseInt(webview.id!)
if ((webview as any).nvue) { if ((webview as any).nvue) {
createNVuePage(parseInt(webview.id!), webview, pageInstance) if (id === 1 && nvuePageVm) {
initNVueEntryPage(webview, nvuePageVm, pageInstance)
} else {
createNVuePage(id, webview, pageInstance)
}
} else { } else {
createVuePage( createVuePage(id, route, query, pageInstance, initPageOptions(routeOptions))
parseInt(webview.id!),
route,
query,
pageInstance,
initPageOptions(routeOptions)
)
} }
return webview return webview
} }
...@@ -146,21 +146,24 @@ function initPageOptions({ meta }: UniApp.UniRoute): PageNodeOptions { ...@@ -146,21 +146,24 @@ function initPageOptions({ meta }: UniApp.UniRoute): PageNodeOptions {
} }
} }
function initNVueEntryPage(webview: PlusWebviewWebviewObject) { function initNVueEntryPage(
const isLaunchNVuePage = webview.id === '1' && (webview as any).nvue webview: PlusWebviewWebviewObject,
nvuePageVm: ComponentPublicInstance,
pageInstance: Page.PageInstance['$page']
) {
initPageVm(nvuePageVm, pageInstance)
addCurrentPage(nvuePageVm)
// 首页是 nvue 时,在 registerPage 时,执行路由堆栈 // 首页是 nvue 时,在 registerPage 时,执行路由堆栈
if (isLaunchNVuePage) { if (
if ( __uniConfig.splashscreen &&
__uniConfig.splashscreen && __uniConfig.splashscreen.autoclose &&
__uniConfig.splashscreen.autoclose && !__uniConfig.splashscreen.alwaysShowBeforeRender
!__uniConfig.splashscreen.alwaysShowBeforeRender ) {
) { plus.navigator.closeSplashscreen()
plus.navigator.closeSplashscreen()
}
__uniConfig.onReady(function () {
navigateFinish()
})
} }
__uniConfig.onReady(function () {
navigateFinish()
})
} }
function createNVuePage( function createNVuePage(
......
...@@ -5,7 +5,7 @@ import path from 'path' ...@@ -5,7 +5,7 @@ import path from 'path'
import fs from 'fs-extra' import fs from 'fs-extra'
import debug from 'debug' import debug from 'debug'
import { transformWithEsbuild } from '@dcloudio/uni-cli-shared' import { removeExt, transformWithEsbuild } from '@dcloudio/uni-cli-shared'
import { nvueOutDir } from '../../utils' import { nvueOutDir } from '../../utils'
import { esbuildGlobals } from '../utils' import { esbuildGlobals } from '../utils'
...@@ -74,11 +74,11 @@ ${ ...@@ -74,11 +74,11 @@ ${
} }
const webview = plus.webview.currentWebview() const webview = plus.webview.currentWebview()
const __pageId = parseInt(webview.id) const __pageId = parseInt(webview.id)
const __pagePath = webview.__path__ const __pagePath = '${removeExt(filename)}'
let __pageQuery = {} let __pageQuery = {}
try{ __pageQuery = JSON.parse(webview.__query__) }catch(e){} try{ __pageQuery = JSON.parse(webview.__query__) }catch(e){}
App.mpType = 'page' App.mpType = 'page'
const app = Vue.createApp(App,{$store:getApp().$store,__pageId,__pagePath,__pageQuery}) const app = Vue.createPageApp(App,{$store:getApp().$store,__pageId,__pagePath,__pageQuery})
app.provide('__globalStyles', Vue.useCssStyles([...AppStyles, ...(App.styles||[])])) app.provide('__globalStyles', Vue.useCssStyles([...AppStyles, ...(App.styles||[])]))
app.mount('#root')`, app.mount('#root')`,
path.join(nvueOutDir(), 'main.js'), path.join(nvueOutDir(), 'main.js'),
......
...@@ -49,7 +49,7 @@ function createApp(code: string) { ...@@ -49,7 +49,7 @@ function createApp(code: string) {
return `${code.replace( return `${code.replace(
'createSSRApp', 'createSSRApp',
'createVueApp as createSSRApp' 'createVueApp as createSSRApp'
)};const {app:__app__,Vuex:__Vuex__,Pinia:__Pinia__}=createApp();uni.Vuex=__Vuex__;uni.Pinia=__Pinia__;__app__._component.mpType='app';__app__._component.render=()=>{};__app__.mount({appendChild(){}});` )};const {app:__app__,Vuex:__Vuex__,Pinia:__Pinia__}=createApp();uni.Vuex=__Vuex__;uni.Pinia=__Pinia__;__app__._component.mpType='app';__app__._component.render=()=>{};__app__.mount('#app');`
} }
function createLegacyApp(code: string) { function createLegacyApp(code: string) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册