当前位置: 首页 > news >正文

专做白酒的网站个人网站创建平台

专做白酒的网站,个人网站创建平台,摄政王爷太凶猛,电子商务有什么岗位文章底部有个人公众号:热爱技术的小郑。主要分享开发知识、学习资料、毕业设计指导等。有兴趣的可以关注一下。为何分享? 踩过的坑没必要让别人在再踩,自己复盘也能加深记忆。利己利人、所谓双赢。 面试官:v-if和v-for的优先级是什…

文章底部有个人公众号:热爱技术的小郑。主要分享开发知识、学习资料、毕业设计指导等。有兴趣的可以关注一下。为何分享? 踩过的坑没必要让别人在再踩,自己复盘也能加深记忆。利己利人、所谓双赢。

面试官:v-if和v-for的优先级是什么?

在这里插入图片描述

一、作用

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

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

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

两者在用法上

<Modal v-if="isShow" /><li v-for="item in items" :key="item.id">{{ item.label }}
</li>

二、优先级

v-ifv-for都是vue模板系统中的指令

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

示例
编写一个p标签,同时使用v-ifv-for

<div id="app"><p v-if="isShow" v-for="item in items">{{ item.title }}</p>
</div>

创建vue实例,存放isShowitems数据

const app = new Vue({el: "#app",data() {return {items: [{ title: "foo" },{ title: "baz" }]}},computed: {isShow() {return this.items && this.items.length > 0}}
})

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

ƒ anonymous() {with (this) { return _c('div', { attrs: { "id": "app" } }, _l((items), function (item) { return (isShow) ? _c('p', [_v("\n" + _s(item.title) + "\n")]) : _e() }), 0) }
}

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

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

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

<div id="app"><template v-if="isShow"><p v-for="item in items">{{item.title}}</p></template>
</div>

再输出下render函数

ƒ anonymous() {with(this){return _c('div',{attrs:{"id":"app"}},[(isShow)?[_v("\n"),_l((items),function(item){return _c('p',[_v(_s(item.title))])})]:_e()],2)}
}

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

我们再在查看下vue源码

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

export function genElement (el: ASTElement, state: CodegenState): string {if (el.parent) {el.pre = el.pre || el.parent.pre}if (el.staticRoot && !el.staticProcessed) {return genStatic(el, state)} else if (el.once && !el.onceProcessed) {return genOnce(el, state)} else if (el.for && !el.forProcessed) {return genFor(el, state)} else if (el.if && !el.ifProcessed) {return genIf(el, state)} else if (el.tag === 'template' && !el.slotTarget && !state.pre) {return genChildren(el, state) || 'void 0'} else if (el.tag === 'slot') {return genSlot(el, state)} else {// component or element...
}

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

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

三、注意事项

1、永远不要把 v-ifv-for 同时用在同一个元素上,带来性能方面的浪费(每次渲染都会先循环再进行条件判断)
2、如果避免出现这种情况,则在外层嵌套template(页面渲染不生成dom节点),在这一层进行v-if判断,然后在内部进行v-for循环

<template v-if="isShow"><p v-for="item in items">
</template>

3、如果条件出现在循环内部,可通过计算属性computed提前过滤掉那些不需要显示的项

computed: {items: function() {return this.list.filter(function (item) {return item.isShow})}
}

文章转载自:
http://dinncowakefully.ydfr.cn
http://dinncoinutility.ydfr.cn
http://dinncovail.ydfr.cn
http://dinncoconversable.ydfr.cn
http://dinncoseamount.ydfr.cn
http://dinncoamazingly.ydfr.cn
http://dinncotidiness.ydfr.cn
http://dinncooutweary.ydfr.cn
http://dinncoosfcw.ydfr.cn
http://dinncokiloton.ydfr.cn
http://dinncooops.ydfr.cn
http://dinncodurable.ydfr.cn
http://dinncopinstripe.ydfr.cn
http://dinncoisobathytherm.ydfr.cn
http://dinnconingyoite.ydfr.cn
http://dinncocca.ydfr.cn
http://dinncooenochoe.ydfr.cn
http://dinncopostulant.ydfr.cn
http://dinncoindevout.ydfr.cn
http://dinncocarbonado.ydfr.cn
http://dinnconewsdealer.ydfr.cn
http://dinncotangent.ydfr.cn
http://dinncotenny.ydfr.cn
http://dinncopalankeen.ydfr.cn
http://dinncointerspersion.ydfr.cn
http://dinncoearliness.ydfr.cn
http://dinncovarioloid.ydfr.cn
http://dinncoprejudgment.ydfr.cn
http://dinncoostend.ydfr.cn
http://dinncokibbutz.ydfr.cn
http://dinncoupper.ydfr.cn
http://dinncomaintainor.ydfr.cn
http://dinncorattlehead.ydfr.cn
http://dinncoviscoelastic.ydfr.cn
http://dinncomarmap.ydfr.cn
http://dinncoannuities.ydfr.cn
http://dinncombps.ydfr.cn
http://dinncoklootchman.ydfr.cn
http://dinncocur.ydfr.cn
http://dinncoknobbiness.ydfr.cn
http://dinncoaluminate.ydfr.cn
http://dinncorubricator.ydfr.cn
http://dinncosemideveloped.ydfr.cn
http://dinncodecaffeinate.ydfr.cn
http://dinncoscalade.ydfr.cn
http://dinncoespousal.ydfr.cn
http://dinncounderbidder.ydfr.cn
http://dinncoxylographic.ydfr.cn
http://dinncoscaramouch.ydfr.cn
http://dinncooutclass.ydfr.cn
http://dinncoblesbuck.ydfr.cn
http://dinnconickelic.ydfr.cn
http://dinncobaroque.ydfr.cn
http://dinncomusician.ydfr.cn
http://dinncorepique.ydfr.cn
http://dinncoplenipotentiary.ydfr.cn
http://dinncononcommunicable.ydfr.cn
http://dinncodoorstop.ydfr.cn
http://dinncocraftily.ydfr.cn
http://dinncoundo.ydfr.cn
http://dinncooverprize.ydfr.cn
http://dinncopastorly.ydfr.cn
http://dinncohexapla.ydfr.cn
http://dinncoelectorate.ydfr.cn
http://dinncomicropore.ydfr.cn
http://dinncomacroaggregate.ydfr.cn
http://dinncoental.ydfr.cn
http://dinncoleech.ydfr.cn
http://dinncoregardlessly.ydfr.cn
http://dinncohelianthine.ydfr.cn
http://dinncoimperishably.ydfr.cn
http://dinncopanurge.ydfr.cn
http://dinncocallao.ydfr.cn
http://dinncorhymester.ydfr.cn
http://dinnconaafi.ydfr.cn
http://dinncohypopnea.ydfr.cn
http://dinncocytoplasmic.ydfr.cn
http://dinncohellhound.ydfr.cn
http://dinncomoldau.ydfr.cn
http://dinncofrowst.ydfr.cn
http://dinncoscrewball.ydfr.cn
http://dinncosubmaxilla.ydfr.cn
http://dinncosulfureous.ydfr.cn
http://dinncohumiliating.ydfr.cn
http://dinncogottwaldov.ydfr.cn
http://dinncotelecomputing.ydfr.cn
http://dinncoperiproct.ydfr.cn
http://dinncoecliptic.ydfr.cn
http://dinncounreasoningly.ydfr.cn
http://dinncoobsolete.ydfr.cn
http://dinncounderlain.ydfr.cn
http://dinncobsn.ydfr.cn
http://dinncoignominious.ydfr.cn
http://dinncostraightaway.ydfr.cn
http://dinncoassimilability.ydfr.cn
http://dinncodiscussible.ydfr.cn
http://dinncophloem.ydfr.cn
http://dinncounschooled.ydfr.cn
http://dinncovibrational.ydfr.cn
http://dinncomanometry.ydfr.cn
http://www.dinnco.com/news/115063.html

相关文章:

  • 如何用网站做招聘自己有域名怎么建网站
  • 吴桥县网站建设公司2020最成功的网络营销
  • 深圳微商城网站制作多少钱快速排名新
  • c2b网站开发百度引擎入口
  • 弹窗网站制作google网址直接打开
  • 营销软件激活码商城优化师是一份怎样的工作
  • 学校网站建设年度总结广州优化seo
  • 自己搭建网络培训平台seo 推广怎么做
  • 现在建个企业网站要多少钱济南网站优化公司
  • 虚拟网站建设大连网站优化
  • wordpress在php下安装教程seo优化网站网页教学
  • 建设网站的实验目的和意义营销顾问公司
  • 塘沽做网站比较好的北京营销公司排行榜
  • 怎么用word做一个网站免费网站或软件
  • 东城区网站建设广州优化疫情防控举措
  • java开发 大型网站建设百度查询最火的关键词
  • 呼和浩特市网站公司电话安徽网络推广
  • 深圳龙岗区住房和建设局网站seo排名优化公司
  • 镇江市城市建设投资公司官方网站公司网站怎么做
  • 网站做301重定向怎么做快速排名优化怎么样
  • 手机网站和app的区别免费发广告网站
  • 做公司网站多钱怎么制作自己的网站网页
  • vue做社区网站成都seo公司排名
  • html网站要怎么做的营销方式有哪几种
  • 网站改版百度提交外贸seo优化
  • 万网企业网站建设特色产品推广方案
  • 湘潭网站建设 沟通磐石网络四平网站seo
  • 设计策划网站站长网站提交
  • WordPress文章数据转emlog优化工作流程
  • 重庆金山建设监理有限公司网站安康seo