MCPcopy Create free account
hub / github.com/Tencent/kbone / init

Method init

packages/miniprogram-render/src/bom/miniprogram.js:31–73  ·  view source on GitHub ↗

* 初始化

(url)

Source from the content-addressed store, hash-verified

29 * 初始化
30 */
31 init(url) {
32 if (typeof url === 'string') this.$_pageUrl = url // 设置真实 url
33 const {
34 origin, entry, router, runtime = {}
35 } = cache.getConfig()
36 const subpackagesMap = runtime.subpackagesMap || {}
37
38 this.$_pageUrl = this.$_pageUrl || (origin + entry)
39 this.$_subpackagesMap = subpackagesMap
40 this.window.location.$$reset(this.$_pageUrl)
41 this.window.history.$$reset()
42
43 if (!pageUrlRouteMap) {
44 // 需要初始化页面 url - 小程序页面路由映射表
45 pageUrlRouteMap = {}
46
47 Object.keys(router).forEach(pageName => {
48 const regexpList = []
49
50 router[pageName].forEach(pathObj => {
51 // 构造正则表达式
52 const regexp = new RegExp(pathObj.regexp, pathObj.options)
53 regexpList.push(regexp)
54 })
55
56 // 将每个页面的路由改造成函数,方便后续做匹配用
57 pageUrlRouteMap[pageName] = pathname => {
58 for (const regexp of regexpList) {
59 const parseRes = regexp.exec(pathname)
60 regexp.lastIndex = 0
61
62 if (parseRes) {
63 // 匹配成功
64 const packageName = subpackagesMap[pageName]
65 return `/${packageName ? packageName + '/' : ''}pages/${pageName}/index`
66 }
67 }
68
69 return null
70 }
71 })
72 }
73 }
74
75 /**
76 * 需要匹配对应路由的 route

Callers 8

onLoadFunction · 0.45
window.test.jsFile · 0.45
createPageFunction · 0.45
location.test.jsFile · 0.45
history.test.jsFile · 0.45
createPageFunction · 0.45
createPageFunction · 0.45
attachedFunction · 0.45

Calls 2

execMethod · 0.80
$$resetMethod · 0.45

Tested by

no test coverage detected