当前位置:网站首页>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 .
边栏推荐
- Returns the lowest common ancestor of two nodes in a binary tree
- 平台入驻与独立部署优缺点对比
- Kubernetes - identity and authority authentication
- Design and implementation of kindergarten management system
- Acwing第 58 场周赛【完结】
- Leetcode42. connect rainwater
- Clean up PHP session files
- Design and practice of kubernetes cluster and application monitoring scheme
- Moco V2 literature research [self supervised learning]
- Class inheritance in C #
猜你喜欢
Bumblebee: build, deliver, and run ebpf programs smoothly like silk
[groovy] loop control (number injection function implements loop | times function | upto function | downto function | step function | closure can be written outside as the final parameter)
Utilisation simple de devtools
Pdf things
[2022 repair version] community scanning code into group activity code to drain the complete operation source code / connect the contract free payment interface / promote the normal binding of subordi
Devtools的简单使用
1.五层网络模型
Why are there fewer and fewer good products produced by big Internet companies such as Tencent and Alibaba?
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Azkaban实战
随机推荐
Kbp206-asemi rectifier bridge kbp206
Good documentation
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
[system security] ten thousand words summary system virtualization container bottom layer principle experiment
Performance of calling delegates vs methods
2. Common request methods
Simple use of devtools
Talk about the SQL server version of DTM sub transaction barrier function
Une question est de savoir si Flink SQL CDC peut définir le parallélisme. Si le parallélisme est supérieur à 1, il y aura un problème d'ordre?
Design and implementation of community hospital information system
Kubernetes - Multi cluster management
In MySQL Association query, the foreign key is null. What if the data cannot be found?
Single box check box
Zabbix
Monitoring web performance with performance
Necessary fonts for designers
平台入驻与独立部署优缺点对比
[deep learning] deep learning reference materials
SQL performance optimization skills
MySQL winter vacation self-study 2022 11 (10)