面试官:为什么Vue中的v-if和v-for不建议一起用?
阅读原文时间:2023年07月08日阅读:2

v-if 指令用于条件性地渲染一块内容。这块内容只会在指令的表达式返回 true值的时候被渲染

v-for 指令基于一个数组来渲染一个列表。v-for 指令需要使用 item in items 形式的特殊语法,其中 items 是源数据数组或者对象,而 item 则是被迭代的数组元素的别名

在 v-for 的时候,建议设置key值,并且保证每个key值是独一无二的,这便于diff算法进行优化

两者在用法上

1
2
3

  • 4 {{ item.label }} 5
  • v-ifv-for都是vue模板系统中的指令

    vue模板编译的时候,会将指令系统转化成可执行的render函数

    示例

    编写一个p标签,同时使用v-if与 v-for

    1
    2

    3 4

    5 6 {{ item.title }} 7 8

    9 10

    创建vue实例,存放isShowitems数据

    1
    2 const app = new Vue({
    3
    4 el: "#app",
    5
    6 data() {
    7
    8 return {
    9
    10 items: [
    11
    12 { title: "foo" },
    13
    14 { title: "baz" }]
    15
    16 }
    17
    18 },
    19
    20 computed: {
    21
    22 isShow() {
    23
    24 return this.items && this.items.length > 0
    25
    26 }
    27
    28 }
    29
    30 })

    模板指令的代码都会生成在render函数中,通过app.$options.render就能得到渲染函数

    1 ƒ anonymous() {
    2
    3 with (this) { return
    4
    5 _c('div', { attrs: { "id": "app" } },
    6
    7 _l((items), function (item)
    8
    9 { return (isShow) ? _c('p', [_v("\n" + _s(item.title) + "\n")]) : _e() }), 0) }
    10
    11 }

    _lvue的列表渲染函数,函数内部都会进行一次if判断

    初步得到结论:v-for优先级是比v-if

    再将v-forv-if置于不同标签

    再输出下render函数

    1
    2 ƒ anonymous() {
    3
    4 with(this){return
    5
    6 _c('div',{attrs:{"id":"app"}},
    7
    8 [(isShow)?[_v("\n"),
    9
    10 _l((items),function(item){return _c('p',[_v(_s(item.title))])})]:_e()],2)}
    11
    12 }

    这时候我们可以看到,v-forv-if作用在不同标签时候,是先进行判断,再进行列表的渲染

    我们再在查看下vue源码

    源码位置:\vue-dev\src\compiler\codegen\index.js

    1
    2 export function genElement (el: ASTElement, state: CodegenState): string {
    3
    4 if (el.parent) {
    5
    6 el.pre = el.pre || el.parent.pre
    7
    8 }
    9
    10 if (el.staticRoot && !el.staticProcessed) {
    11
    12 return genStatic(el, state)
    13
    14 } else if (el.once && !el.onceProcessed) {
    15
    16 return genOnce(el, state)
    17
    18 } else if (el.for && !el.forProcessed) {
    19
    20 return genFor(el, state)
    21
    22 } else if (el.if && !el.ifProcessed) {
    23
    24 return genIf(el, state)
    25
    26 } else if (el.tag === 'template' && !el.slotTarget && !state.pre) {
    27
    28 return genChildren(el, state) || 'void 0'
    29
    30 } else if (el.tag === 'slot') {
    31
    32 return genSlot(el, state)
    33
    34 } else {
    35
    36 // component or element
    37
    38 …
    39
    40 }

    在进行if判断的时候,v-for是比v-if先进行判断

    最终结论:v-for优先级比v-if

    1. 永远不要把 v-if 和 v-for 同时用在同一个元素上,带来性能方面的浪费(每次渲染都会先循环再进行条件判断)

    2. 如果避免出现这种情况,则在外层嵌套template(页面渲染不生成dom节点),在这一层进行v-if判断,然后在内部进行v-for循环

      1
      2
      7 如果条件出现在循环内部,可通过计算属性computed提前过滤掉那些不需要显示的项
      8
      9
      10 computed: {
      11
      12 items: function() {
      13
      14 return this.list.filter(function (item) {
      15
      16 return item.isShow
      17
      18 })
      19
      20 }
      21
      22 }

    https://vue3js.cn/docs/zh

    转载至:https://blog.csdn.net/weixin_44475093/article/details/110607035

    手机扫一扫

    移动阅读更方便

    阿里云服务器
    腾讯云服务器
    七牛云服务器

    你可能感兴趣的文章