当前位置:网站首页>v-if VS v-show 2.0
v-if VS v-show 2.0
2022-07-05 03:27:00 【Look at the universe】
v-show And v-if The difference between
- Different means of control
- The compilation process is different
- Compilation conditions are different
Control means : v-show The operation that hides this element is css, bring display:none,dom The elements are still there ; however v-if Hiding this element is directly from dom Node delete element .
The build process : v-if The switch has a partial compilation / Uninstallation process , The switching process appropriately destroys and rebuilds internal event listeners and subcomponents .v-show It's simply based on css Switch .
Compile conditions : v-if Is the real conditional rendering , It ensures that the event listeners and subcomponents in the condition block are properly destroyed and rebuilt during switching , Only when the rendering condition is false , Do not operate , Not until it's true .
v-show from false Turn into true The component life cycle will not be triggered when
v-if from false Turn into true When , Trigger component beforeCreate、create、beforeMount、mounted hook , from true Turn into false When the component is triggered beforeDestory、destoryed Method Performance consumption :v-if Higher switching consumption ;v-show There is a higher initial render cost ;
v-if Relearn the reusability of elements
As mentioned in the previous article Vue Render elements as efficiently as possible , Often reuse existing elements rather than rendering from scratch .
In use at this time v-if When switching, we found , When switching between two tags with basically the same content , Here is an example element-ui The form elements inside <el-form>
<!-- Mobile number registration -->
<view v-show="isPhone">
<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
<el-form-item v-if="isPhone":label="i18n.sjhm" prop="phone">
<el-input v-model="ruleForm.phone" :placeholder="i18n.qtxndsjh" class="rule-input">
<el-select v-model="phoneArea" slot="prepend" class="num-style">
<el-option label="+86 mainland " value="+86"></el-option>
<el-option label="+852 Hong Kong " value="+852"></el-option>
<el-option label="+853 Macau " value="+853"></el-option>
</el-select>
</el-input>
</el-form-item>
<el-form-item :label="i18n.yzm" prop="code">
<el-input v-model="ruleForm.code" :placeholder="i18n['qsryzm']"></el-input>
<el-button type="text" v-if="!phone_change" class="get-code" @click="openTestCode">{
{i18n.hqyzm}}</el-button>
<el-button type="text" v-if="phone_change" class="get-code" disabled>{
{time}}s{
{i18n.hcxhq}}</el-button>
</el-form-item>
<el-form-item :label="i18n.password" prop="password">
<el-input type="password" v-model="ruleForm.password" :placeholder="i18n.qszndmm" auto-complete="off"></el-input>
</el-form-item>
<el-form-item :label="i18n.qrmm" prop="again_password">
<el-input type="password" v-model="ruleForm.again_password" :placeholder="i18n.qqrndmm" auto-complete="off"></el-input>
</el-form-item>
<el-form-item>
<el-button @click="resetForm()">{
{i18n.return}}</el-button>
<el-button type="primary" @click="register()">{
{i18n.ljzc}}</el-button>
</el-form-item>
</el-form>
</view>
<!-- Email registration -->
<view v-show="!isPhone">
<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
<el-form-item v-if="!isPhone" :label="i18n.yxyx" prop="email">
<el-input v-model="ruleForm.email" :placeholder="i18n.qtxndyx"></el-input>
</el-form-item>
<el-form-item :label="i18n.yzm" prop="code">
<el-input v-model="ruleForm.code" :placeholder="i18n['qsryzm']"></el-input>
<el-button type="text" v-if="!phone_change" class="get-code" @click="openTestCode">{
{i18n.hqyzm}}</el-button>
<el-button type="text" v-if="phone_change" class="get-code" disabled>{
{time}}s{
{i18n.hcxhq}}</el-button>
</el-form-item>
<el-form-item :label="i18n.password" prop="password">
<el-input type="password" v-model="ruleForm.password" :placeholder="i18n.qszndmm" auto-complete="off"></el-input>
</el-form-item>
<el-form-item :label="i18n.qrmm" prop="again_password">
<el-input type="password" v-model="ruleForm.again_password" :placeholder="i18n.qqrndmm" auto-complete="off"></el-input>
</el-form-item>
<el-form-item>
<el-button @click="resetForm()">{
{i18n.return}}</el-button>
<el-button type="primary" @click="register()">{
{i18n.ljzc}}</el-button>
</el-form-item>
</el-form>
</view>
We can see here what I use v-show To switch instead of v-if, Because use v-if When , because <el-form>
Inside <el-form-item>
Basically, it is the format of input box , This is the use of v-if When switching, elements will be reused , At this time, the red prompt text and style of the first form judgment input box will not be changed in time with the switching of the upper label , Unless we click the input box on the email registration side again, we will make out of focus judgment , Then show the prompt of please enter email .
Use v-show This will not happen , because v-show It is the overall switch between two large forms , There are no reusable elements .
边栏推荐
- Bumblebee: build, deliver, and run ebpf programs smoothly like silk
- Returns the lowest common ancestor of two nodes in a binary tree
- this+闭包+作用域 面试题
- Multi person online anonymous chat room / private chat room source code / support the creation of multiple chat rooms at the same time
- Simple use of devtools
- In MySQL Association query, the foreign key is null. What if the data cannot be found?
- 問下,這個ADB mysql支持sqlserver嗎?
- [groovy] string (string splicing | multi line string)
- There is a question about whether the parallelism can be set for Flink SQL CDC. If the parallelism is greater than 1, will there be a sequence problem?
- Delphi free memory
猜你喜欢
Zabbix
Voice chip wt2003h4 B008 single chip to realize the quick design of intelligent doorbell scheme
Simple use of devtools
Design and implementation of community hospital information system
[groovy] groovy environment setup (download groovy | install groovy | configure groovy environment variables)
Pytest (4) - test case execution sequence
Port, domain name, protocol.
Design and implementation of kindergarten management system
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Anchor free series network yolox source code line by line explanation Part 2 (a total of 10, ensure to explain line by line, after reading, you can change the network at will, not just as a participan
随机推荐
Kubernetes -- cluster expansion principle
How to learn to get the embedding matrix e # yyds dry goods inventory #
[groovy] string (string type variable definition | character type variable definition)
Tencent cloud, realize image upload
問下,這個ADB mysql支持sqlserver嗎?
Breaking the information cocoon - my method of actively obtaining information - 3
SQL performance optimization skills
2021 Li Hongyi machine learning (3): what if neural network training fails
Sqoop command
Six stone programming: advantages of automated testing
Last words record
[105] Baidu brain map - Online mind mapping tool
【微服务|SCG】Filters的33种用法
端口,域名,协议。
Bumblebee: build, deliver, and run ebpf programs smoothly like silk
[200 opencv routines] 99 Modified alpha mean filter
LeetCode146. LRU cache
Acwing第 58 场周赛【完结】
SQL injection exercise -- sqli Labs
[groovy] groovy environment setup (download groovy | install groovy | configure groovy environment variables)