Skip to content

Commit a4210a1

Browse files
committed
chore: 添加异步组件注册涉及代码流程注释说明
1 parent c02f29d commit a4210a1

File tree

8 files changed

+185
-16
lines changed

8 files changed

+185
-16
lines changed

Diff for: dist/vue.js

+64-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: examples/00-vue-analysis/12-async-component.html

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ <h1>12-async-component</h1>
1010
<script>
1111
// 示例参看 examples/vue-cli-vue2.6-project 工程!
1212
// 相关注释也在工程中注明了!
13-
13+
// 异步组件的代码执行也会先经过 src\core\vdom\create-component.js 文件中 createComponent 函数的处理
14+
// 1.异步组件3种实现方式,涉及的核心代码在 src\core\vdom\helpers\resolve-async-component.js 文件中的 resolveAsyncComponent 函数
15+
// (1)普通函数异步组件,通过 工厂函数 实现
16+
// (2)Promise 异步组件,通过 import() 语法实现
17+
// (3)高级异步组件,通过 对象配置 实现
18+
// 2.通常情况下,异步组件实现的本质是2次渲染,先渲染成注释节点,当组件加载成功后,再通过forceRender重新渲染。
19+
// 3.异步组件3种实现方式中,高级异步组件的设计是非常巧妙的,它可以通过简单的配置实现了loading、resolve、reject、timeout4种状态
1420
</script>
1521
</body>
1622
</html>

Diff for: examples/vue-cli-vue2.6-project/src/App.vue

+1-7
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,8 @@
66
</template>
77

88
<script>
9-
import HelloWorld from "./components/HelloWorld.vue";
10-
119
export default {
12-
name: "App",
13-
components: {
14-
// 局部注册组件
15-
HelloWorld,
16-
},
10+
name: "App"
1711
};
1812
</script>
1913

Diff for: examples/vue-cli-vue2.6-project/src/main.js

+49-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,55 @@
11
import Vue from '../../../dist/vue'
22
import App from './App.vue'
33

4-
// 全局注册组件
5-
Vue.component('app', App)
4+
// 官网链接:https://v2.cn.vuejs.org/v2/guide/components-dynamic-async.html#%E5%BC%82%E6%AD%A5%E7%BB%84%E4%BB%B6
5+
6+
// 异步组件注册方式1:工厂函数
7+
// eslint-disable-next-line
8+
// Vue.component('HelloWorld', function (resolve, reject) {
9+
// // 这个特殊的 require 语法告诉 webpack
10+
// // 自动将编译后的代码分割为不同的 chunk
11+
// // 执行时会发送网络请求,网络响应成功后会执行这里传入的第 2 个回调函数
12+
// require(['./components/HelloWorld'], function (res) {
13+
// resolve(res)
14+
// })
15+
// })
16+
17+
// 异步组件注册方式2:import() 语法
18+
// Vue.component(
19+
// 'HelloWorld',
20+
// // 该 `import` 函数返回一个 Promise 对象
21+
// () => import('./components/HelloWorld')
22+
// )
23+
24+
// 异步组件注册方式3:对象配置
25+
const LoadingComp = {
26+
template: '<div>loading</div>'
27+
}
28+
29+
const ErrorComp = {
30+
template: '<div>error</div>'
31+
}
32+
33+
const AsyncComp = () => ({
34+
// 需要加载的组件 (应该是一个 `Promise` 对象)
35+
component: import('./components/HelloWorld'),
36+
// 异步组件加载时使用的组件
37+
loading: LoadingComp,
38+
// 加载失败时使用的组件
39+
error: ErrorComp,
40+
// 说明:network 面板中把带宽调小测试效果更明显!
41+
// 展示 加载时组件 的延时时间。默认值是 200 (毫秒)
42+
// 即到了该延时时间才出现 loading 属性对应的组件,如果 component 属性对应的组件请求在该 延时时间 内响应回来了,则 加载时组件 不会出现在页面上!
43+
delay: 200,
44+
// 如果提供了超时时间且 component 属性对应组件加载也超时了,则使用加载失败时使用的error属性对应组件。
45+
// 默认值是:`Infinity`,即等价于不传入 timeout 属性
46+
// 如果要让该 AsyncComp 最终显示为 ErrorComp,则可以在 network 面板中设置“Slow 3G”
47+
timeout: 3000
48+
})
49+
50+
Vue.component('HelloWorld', AsyncComp)
651

752
new Vue({
853
el: '#app',
9-
template: '<app></app>'
10-
})
54+
render: h => h(App)
55+
})

Diff for: src/core/vdom/create-component.js

+1
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ export function createComponent (
161161
// as a comment node but preserves all the raw information for the node.
162162
// the information will be used for async server-rendering and hydration.
163163
/*如果这是一个异步组件则会不会返回任何东西(undifiened),直接return掉,等待回调函数去触发父组件更新。s*/
164+
// 创建一个注释节点 VNode 实例
164165
return createAsyncPlaceholder(
165166
asyncFactory,
166167
data,

0 commit comments

Comments
 (0)