Conversation
|
应用级使用方法: // app.js
import { runApp } from 'rax-app';
import appConfig from './app.json';
runApp(appConfig, {
dynamicConfig: true,
onLaunch(options) {
},
onShow() {
},
onHide() {
},
onError(err) {
},
onTabItem({ from, path, text, index }) {
}
}); |
|
页面级使用方法: // 函数组件
import { createElement } from 'rax';
import { usePageShow } from 'rax-app';
function Home() {
usePageShow(() => {
// 首次调用,回调函数会在组件完成渲染后执行
});
usePageHide(() => {
});
}
// Class Component
import { createElement, Component } from 'rax';
import { withPageLifeCycle } from 'rax-app';
class Home extends Component {
onShow() {
// 首次调用,函数在组件 constructor 之后调用
}
}
export default withPageLifeCycle(Home); |
| // In MiniApp platform show event will trigger after addPageLifeCycle, so it needn't be execute in constructor | ||
| this.onShow(); | ||
| } | ||
| addPageLifeCycle(SHOW, this.onShow.bind(this)); |
There was a problem hiding this comment.
如果是 !isMiniAppPlatform 会执行两边 onShow?
There was a problem hiding this comment.
时机不同,在 !isMiniAppPlatform 的环境下,constructor 的时候已经错过了页面 onShow 的时机,这里是最早执行 onShow 的时候了
|
以下功能下一期添加:
|
| @@ -0,0 +1,12 @@ | |||
| import { isMiniAppPlatform } from './env'; | |||
|
|
|||
| let history; | |||
There was a problem hiding this comment.
路由这块可以考虑将 createHistory 的逻辑从 runApp 也抽离到这个文件,是否更合理一些
packages/rax-app/src/router.js
Outdated
|
|
||
| let withRouter; | ||
|
|
||
| if (isMiniApp || isWeChatMiniProgram || isByteDanceMicroApp) { |
There was a problem hiding this comment.
可以直接使用上面封装的 isMiniAppPlatform ?https://github.com/alibaba/rax/pull/1927/files#diff-307cc0431c8b94324dcb47f5d0595ff4R3
packages/rax-app/src/router.js
Outdated
| @@ -0,0 +1,30 @@ | |||
| import { createElement } from 'rax'; | |||
| import { withRouter as webSPAWithRouter } from 'rax-use-router'; | |||
There was a problem hiding this comment.
这个 router 不只是针对 webSPA 的吧 ?如果是感觉不太语义。
packages/rax-app/src/router.js
Outdated
| import { withRouter as webSPAWithRouter } from 'rax-use-router'; | ||
| import { isMiniApp, isWeChatMiniProgram, isByteDanceMicroApp } from 'universal-env'; | ||
|
|
||
| let withRouter; |
There was a problem hiding this comment.
直接 let withRouter = webSPAWithRouter 下面逻辑就不用 else 了
| throw new Error('Error: the runApp method second param can only be Object.'); | ||
| } | ||
|
|
||
| const pageProps = handleDynamicConfig(dynamicConfig); |
There was a problem hiding this comment.
这里有点没理解,如果 dynamicConfig 为 false,为什么要把 dynamicConfig 当做 pageProps,场景是 ?
There was a problem hiding this comment.
这个是兼容以前的逻辑,以前 runApp 第二个参数是 pageProps,后面 raxjs 就直接干掉这个逻辑就好了
packages/rax-app/src/runApp.js
Outdated
|
|
||
| // In MiniApp, it needn't return App Component | ||
| if (isMiniApp || isWeChatMiniProgram) { | ||
| } else if (isMiniApp || isWeChatMiniProgram || isByteDanceMicroApp) { |
There was a problem hiding this comment.
使用 isMiniAppPlatform,其他地方也可用统一改下
| window.addEventListener('appshare', ({ context, shareInfo, options }) => { | ||
| emit(SHARE, context, shareInfo, options); | ||
| }); | ||
| window.addEventListener('tabitemclick', ({ options, context }) => { |
There was a problem hiding this comment.
window 在 SSR 下是无效的,需要考虑下吗
packages/rax-app/src/app.js
Outdated
| // Emit app hide | ||
| emit(HIDE); | ||
| // Emit page hide | ||
| pageEmit(HIDE, router.current.path); |
There was a problem hiding this comment.
这个文件相关的 pageEmit 是不是放到 page.js 合理些 ?
There was a problem hiding this comment.
这个是 history.listen 回调的逻辑,可能没法拆过去
packages/rax-app/src/app.js
Outdated
| } | ||
| } | ||
| }); | ||
| // Emit Web lifeCycles |
There was a problem hiding this comment.
Emit error lifeCycles ?
|
|
||
| for (let i = 0, l = routes.length; i < l; i++) { | ||
| if (fullpath === routes[i].path || routes[i].regexp && routes[i].regexp.test(fullpath)) { | ||
| initialComponent = routes[i].component; |
There was a problem hiding this comment.
这边所有的页面都包了一层 Wrapper :
const importComponent = `() => () => interopRequire(require('${getDepPath(route.source, this.rootContext)}'))`;
packages/rax-app/src/app.js
Outdated
| // Get history | ||
| const history = getHistory(); | ||
| // The app switches from foreground to background | ||
| if (router.current && history.pathname === router.current.path) { |
There was a problem hiding this comment.
history 上应该没有 pathname,这里是 history.location.pathname ?
packages/rax-app/src/runApp.js
Outdated
| // Flow router info | ||
| router.prev = router.current; | ||
| router.current = { | ||
| path: location.pathname, |
There was a problem hiding this comment.
这里改成的 key 是 pathname 会不会更语义点,在对比的时候也是 history.location.pathname === router.current.pathname ?
| console.log('@weex-module/globalEvent error: ' + err); | ||
| } | ||
| } else if (!isUndef(document) && !isUndef(window)) { | ||
| document.addEventListener('visibilitychange', function() { |
There was a problem hiding this comment.
visibilitychange 事件现在还是 Recommendation 阶段,Safari 并未完全按照规范实现,即在 Safari 切换 Tab 时不会触发
|
|
||
| // Emit app launch cycle. | ||
| emit('launch'); | ||
| appEmit(LAUNCH); |
There was a problem hiding this comment.
There was a problem hiding this comment.
小程序自己有 app lanuch 的钩子,我用 window trigger 的
|
|
||
| export default function runApp(appConfig, pageProps = {}) { | ||
| function handleDynamicConfig(config) { | ||
| if (config.dynamicConfig) { |
There was a problem hiding this comment.
没有 config.dynamicConfig 就先 return 吧

onLaunch/onErrorwithPageLifeCyclewithRouter