【发布时间】:2021-01-16 04:04:30
【问题描述】:
我正在使用 Vue.js 框架制作一个小型原型应用程序,但遇到了问题。 我想始终显示带有显示按钮的输入字段,但有条件地显示一些文本。听起来很简单,但当我尝试这个时,它只显示一个空白页。这是我的组件代码:
<template>
<div class="box">
<input v-model="password" :type = "show === false ? 'text' : 'password'" placeholder="Password">
<button type="button" @click="show = ! show">Show</button>
Your password was scored {{ strengthPercentage }}%
<div v-if="strengthLevel === 1">Your password is very easy to guess. Add some more characters.</div>
<div v-if="strengthLevel === 2">Your password is not very strong. Add some variations.</div>
<div v-if="strengthLevel === 3">Your password is quite good. You could still use some more variations though</div>
<div v-if="strengthLevel === 4">Your password is strong enough. Feel free to use it!</div>
</div>
</template>
<script>
export default {
name: "PasswordField",
data: function() {
return {
password : '',
show : false,
}
},
computed: {
strengthPercentage() {
let score = 0;
if(this.password == ""){ return score; }
for(let i = 0; i<this.password.length; i++){
if(i<7) {
score += 5;
} if(i<9){
score += 2;
}
}
let variations = {
digits: /\d/.test(this.password),
lowercase: /[a-z]/.test(this.password),
uppercase: /[A-Z]/.test(this.password),
special: /\W/.test(this.password)
}
let variationCount = 0;
for(let check in variations){
if(variations[check] === true){ variationCount++; }
}
score += (variationCount - 1) * 10;
if(score>100){ score = 100;}
return Math.ceil(parseInt(score));
},
strengthLevel(){
let percentage = this.strengthPercentage();
if(percentage < 30) { return 1; }
if(percentage < 60) { return 2; }
if(percentage < 80) { return 3; }
else { return 4; }
}
}
}
</script>
<style scoped>
</style>
有什么想法吗?
【问题讨论】:
标签: javascript html vue.js components webstorm