当前位置:网站首页>用户登录切换案例
用户登录切换案例
2022-07-27 18:12:00 【宇智波-林中路】
1.定义类型
1.1v-if
小问题:
如果我们在有输入内容的情况下,切换了类型,我们会发现文字依然显示之前的输入的内容。
但是按道理讲,我们应该切换到另外一个input元素中了。
在另一个input元素中,我们并没有输入内容。 为什么会出现这个问题呢?
问题解答:
这是因为Vue在进行DOM渲染时,出于性能考虑,会尽可能的复用已经存在的元素,而不是重新创建新的元素。
在上面的案例中,Vue内部会发现原来的input元素不再使用,直接作为else中的input来使用了。 解决方案:
如果我们不希望Vue出现类似重复利用的问题,可以给对应的input添加key
并且我们需要保证key的不同
<body>
<div id="app">
<span v-if="type=='username' ">
<label for="username">账户登录:</label>
<input
type="text"
id="username"
placeholder="账户登录"
key="username"
/>
</span>
<span v-else>
<label for="email">邮箱登录:</label>
<input type="text" id="email" placeholder="邮箱登录" key="email" />
</span>
<button @click="handleChangeType">切换类型</button>
</div>
</body>
<script>
const app = new Vue({
el: "#app",
data: {
type: "username",
},
methods: {
handleChangeType() {
this.type = this.type == "username" ? "email" : "username";
},
},
});
</script>1.2.v-show
<body>
<div id="app">
<span v-show="type=='username' ">
<label for="username">账户登录:</label>
<input type="text" id="username" placeholder="账户登录" />
</span>
<span v-show="type=='email' ">
<label for="email">邮箱登录:</label>
<input type="text" id="email" placeholder="邮箱登录" />
</span>
<button @click="handleChangeType">切换类型</button>
</div>
</body>
<script>
const app = new Vue({
el: "#app",
data: {
type: "username",
},
methods: {
handleChangeType() {
this.type = this.type == "username" ? "email" : "username";
},
},
});
</script>2.定义布尔值
2.1 v-if
<body>
<div id="app">
<input type="button" value="显示登录" @click="flag=true" />
<input type="button" value="显示注册" @click="flag=false" />
<div v-if="flag">
<input type="text" placeholder="账户" /> <br />
<input type="text" placeholder="密码" /> <br />
<input type="button" value="登录" />
</div>
<div v-else>
<input type="text" placeholder="用户名" /> <br />
<input type="text" placeholder="电话" /> <br />
<input type="button" value="注册" />
</div>
</div>
</body> <script>
const app = new Vue({
el: "#app",
data: {
flag: true,
},
});
</script>
2.2v-show
<body>
<div id="app">
<input type="button" value="显示登录" @click="flag=true" />
<input type="button" value="显示注册" @click="flag=false" />
<div v-show="flag==true">
<input type="text" placeholder="账户" /> <br />
<input type="text" placeholder="密码" /> <br />
<input type="button" value="登录" />
</div>
<div v-show="flag == false">
<input type="text" placeholder="用户名" /> <br />
<input type="text" placeholder="电话" /> <br />
<input type="button" value="注册" />
</div>
</div>
</body> <script>
const app = new Vue({
el: "#app",
data: {
flag: true,
},
});
</script>边栏推荐
- To do the test, you have to go to the big factory and disclose the "hidden rules" of bat big factory recruitment internally
- Flask Mdict builds online MDICT Dictionary Service
- What configurations are required to connect polardb and redis?
- [Alibaba security × ICDM 2022] 200000 bonus pool! The risk commodity inspection competition on the large-scale e-commerce map is in hot registration
- Ten year test old bird talk about mobile terminal compatibility test
- 用户和权限创建普通用户
- Interviewer: what is the abstract factory model?
- LabVIEW学习笔记五:按钮按下后无法返回原状
- 做测试, 就得去大厂,内部披露BAT大厂招聘“潜规则”
- 未定义变量 “Lattice“ 或类 “Lattice.latticeEasy“(Matlab)
猜你喜欢
![[Alibaba security × ICDM 2022] 200000 bonus pool! The risk commodity inspection competition on the large-scale e-commerce map is in hot registration](/img/38/9fadea0d37053a3ebb73806a9963f1.jpg)
[Alibaba security × ICDM 2022] 200000 bonus pool! The risk commodity inspection competition on the large-scale e-commerce map is in hot registration

How to solve the problem of missing alarm information and synchronization when Haikang equipment is connected to easycvr?

2022-07-19 advanced network engineering (XX) BGP route optimization, route optimization analysis one by one

Introduction to JVs Foundation

access control

adb shell ls /system/bin(索引表)

Check the internship salary of Internet companies: with it, you can also enter the factory
![[benefit activity] stack a buff for your code! Click](/img/2d/dabf0ad5d7bd553dada5921abf6c06.png)
[benefit activity] stack a buff for your code! Click "tea" to receive the gift

【深度学习】Pytorch Tensor 张量

Standing on the shoulders of giants to learn, jd.com's popular architect growth manual was launched
随机推荐
Koin simple to use
What configurations are required to connect polardb and redis?
Lennix Lai, OKx financial market director: Web3 is a revolution
浅析即时通讯移动端 IM 开发中登录请求的优化
【深度学习】Pytorch Tensor 张量
Mongodb learning notes: bson structure analysis
antdv: Each record in table should have a unique `key` prop,or set `rowKey` to an unique primary key
greedy
You can understand it at a glance, eslint
JVS私有化部署启动失败处理方案
学术分享 | 清华大学 康重庆:电力系统碳计量技术与应用(Matlab代码实现)
【毕设教程】YOLOv7 目标检测网络解读
Redis basic understanding, five basic data types
Adjust the array so that odd numbers all precede even numbers
Using dataX to realize efficient synchronization of MySQL data
How bad can a programmer be?
User and authority modify user password
Scrollintoview realizes simple anchor location (example: select city list)
openresty lua-resty-core 使用
Preprocessing and macro definition
