search

Home  >  Q&A  >  body text

vue.js default value, how to add it

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>

<div id="box">
input里面输入值,从而改变结果<br>
{{num1}}+<input type="text" v-model="num2">={{num3}}
</div>
<script src="js/Vue.js"></script>
<script>
new Vue({
el:'#box',
data:{
num1:'1',
num2:'2',
num3:''
},
watch:{
num2:function(){
return this.num3=Math.floor(this.num1)+Math.floor(this.num2);

}
}
})
</script>
</body>
</html>


2019-01-31_115118.pngThe default is like this. How can I directly output the value of num3 when opening the page?

善為善為2143 days ago1183

reply all(2)I'll reply

  • 天蓬老师

    天蓬老师2019-02-13 14:09:25

    To convert the numbers in the string to numeric type, you should use parseInt() / parseFloat()

    reply
    0
  • Cancelreply