Skip to content

Commit 17e7618

Browse files
committed
Merge branch 'master' of github.com:57code/vue-interview
2 parents 1f6dc58 + 6bcfb67 commit 17e7618

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
## 目录
1313
1. [v-if和v-for哪个优先级更高?](public/01-vif-vfor/README.md)
1414
2. [你知道key的作用吗?](public/02-key/README.md)
15-
3. [能说说双向绑定以及它的实现原理吗?](public/03/README.md)
15+
3. [能说说双向绑定以及它的实现原理吗?](public/03-v-model/README.md)
1616
4. [你了解diff算法吗?](public/04-diff/README.md)
1717
5. [vue中组件之间的通信方式?](public/05-communication/README.md)
1818
6. [简单说一说你对vuex理解?](public/06/README.md)

public/01-vif-vfor/README.md

+27-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## v-if和v-for哪个优先级更高?
22

3-
分析:此题考查常识,文档中曾有[详细说明](https://cn.vuejs.org/v2/style-guide/#%E9%81%BF%E5%85%8D-v-if-%E5%92%8C-v-for-%E7%94%A8%E5%9C%A8%E4%B8%80%E8%B5%B7%E5%BF%85%E8%A6%81);也是一个很好的实践题目,项目中经常会遇到,能够看出面试者应用能力。
3+
分析:此题考查常识,文档中曾有详细说明[v2](https://cn.vuejs.org/v2/style-guide/#%E9%81%BF%E5%85%8D-v-if-%E5%92%8C-v-for-%E7%94%A8%E5%9C%A8%E4%B8%80%E8%B5%B7%E5%BF%85%E8%A6%81)|[v3](https://vuejs.org/style-guide/rules-essential.html#avoid-v-if-with-v-for);也是一个很好的实践题目,项目中经常会遇到,能够看出面试者应用能力。
44

55

66

@@ -18,7 +18,7 @@
1818

1919
回答范例:
2020

21-
1. v-for优先于v-if被解析
21+
1. `Vue 2` 中,`v-for` 优先于 `v-if` 被解析;但在 `Vue 3` 中,则完全相反,`v-if` 的优先级高于 `v-for`
2222

2323
2. 我曾经做过实验,把它们放在一起,输出的渲染函数中可以看出会先执行循环再判断条件
2424

@@ -40,7 +40,7 @@
4040

4141
知其所以然:
4242

43-
做个测试,[01.html](./01.html)
43+
`Vue 2` 中做个测试,[test.html](./test.html)
4444
两者同级时,渲染函数如下:
4545

4646

@@ -51,7 +51,30 @@ with(this){return _c('div',{attrs:{"id":"app"}},_l((items),function(item){return
5151
}
5252
```
5353

54+
`Vue 3` 中做个测试,[test-v3.html](./test-v3.html)
55+
两者同级时,渲染函数如下:
56+
57+
```js
58+
(function anonymous(
59+
) {
60+
const _Vue = Vue
61+
62+
return function render(_ctx, _cache) {
63+
with (_ctx) {
64+
const { renderList: _renderList, Fragment: _Fragment, openBlock: _openBlock, createElementBlock: _createElementBlock, toDisplayString: _toDisplayString, createCommentVNode: _createCommentVNode } = _Vue
65+
66+
return shouldShowUsers
67+
? (_openBlock(true), _createElementBlock(_Fragment, { key: 0 }, _renderList(items, (item) => {
68+
return (_openBlock(), _createElementBlock("div", { key: item.id }, _toDisplayString(item.name), 1 /* TEXT */))
69+
}), 128 /* KEYED_FRAGMENT */))
70+
: _createCommentVNode("v-if", true)
71+
}
72+
}
73+
})
74+
```
5475

5576

56-
源码中找答案compiler/codegen/index.js
77+
源码中找答案:
78+
`Vue 2`[compiler/codegen/index.js](https://github1s.com/vuejs/vue/blob/dev/src/compiler/codegen/index.js#L65-L69)
79+
`Vue 3`[compiler-core/src/codegen.ts](https://github1s.com/vuejs/core/blob/main/packages/compiler-core/src/codegen.ts#L586-L587)
5780

public/01-vif-vfor/test-v3.html

+12-3
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,34 @@
88
</head>
99

1010
<body>
11-
<div id="app">
12-
<!-- 过滤列表中项目 -->
11+
<!-- 过滤列表中项目 -->
12+
<!-- 浏览器控制台会报错:Uncaught TypeError: Cannot read properties of undefined (reading 'isActive') -->
13+
<!-- <div id="app">
1314
<div v-for="item in items" :key="item.id" v-if="item.isActive">
1415
{{ item.name }}
1516
</div>
17+
</div> -->
18+
19+
<!-- 避免渲染应该被隐藏的列表 -->
20+
<div id="app">
21+
<div v-for="item of items" :key="item.id" v-if="shouldShowUsers">
22+
{{ item.name }}
23+
</div>
1624
</div>
1725
<script src="http://unpkg.com/vue@3"></script>
1826
<script>
1927
const app = Vue.createApp({
2028
data() {
2129
return {
30+
shouldShowUsers: true,
2231
items: [
2332
{ id: 1, name: '张三', isActive: true },
2433
{ id: 2, name: '李四', isActive: false }
2534
]
2635
}
2736
},
2837
}).mount('#app')
29-
console.log(app.$options.render);
38+
console.log(app.$options.render);
3039
</script>
3140
</body>
3241

0 commit comments

Comments
 (0)