效果图:
![]()
1.html代码

<div class="form-group" style="display: flex;">
<div>
<span>验证码:</span>
<input type="text" id="code" v-model="code" class="code" placeholder="请输入您的验证码" />
</div>
<div class="login-code" @click="refreshCode">
<!--验证码组件-->
<s-identify :identifyCode="identifyCode"></s-identify>
</div>
</div>2.css样式

/*验证码样式*/
.code{
width:124px;
height:31px;
border:1px solid rgba(186,186,186,1);
}
.login-code{
cursor: pointer;
}CSS 代码
3.js引入验证码组件,并且定义三个变量。

import SIdentify from '../components/sidentify'components: { SIdentify },
data () {
return {
identifyCodes: "1234567890",
identifyCode: "",
code:"",//text框输入的验证码
}
},
引入验证码组件,以及需要定义的变量
4.mounted里的代码

mounted(){
this.identifyCode = "";
this.makeCode(this.identifyCodes, 4);
},mounted代码
5.在created里初始化验证码

6.methods里添加以下方法。

//验证码
randomNum(min, max) {
return Math.floor(Math.random() * (max - min) + min);
},refreshCode() {
this.identifyCode = "";
this.makeCode(this.identifyCodes, 4);
},
makeCode(o, l) {
for (let i = 0; i < l; i++) {
this.identifyCode += this.identifyCodes[
this.randomNum(0, this.identifyCodes.length)
];
}
console.log(this.identifyCode);
},










