<html>
<head>
<title>Vue.js Watch Property</title>
<link rel="stylesheet" href="index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id = "wat_pro">
Kilometers : <input type = "text" v-model = "kilometers"><br/>
Meters : <input type = "text" v-model = "meters">
</div>
<script src="index.js"></script>
</body>
</html>
var vm = new Vue({
el: '#wat_pro',
data: {
kilometers : 0,
meters:0
},
methods: {
},
computed :{
},
watch : {
kilometers:function(val) {
this.kilometers = val;
this.meters = val * 1000;
},
meters : function (val) {
this.kilometers = val/ 1000;
this.meters = val;
}
}
})
html, body {
margin: 5px;
padding: 0;
}
<html>
<head>
<title>Vue.js Watch Property</title>
<link rel="stylesheet" href="index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="eg_1">{{ fullName }}</div>
<script src="index.js"></script>
</body>
</html>
var vm = new Vue({
el: '#eg_1',
data: {
firstName: 'Alex',
lastName: 'Panda',
fullName: 'Alex Panda'
},
watch: {
firstName: function (val) {
this.fullName = val + ' ' + this.lastName
},
lastName: function (val) {
this.fullName = this.firstName + ' ' + val
}
}
})
<html>
<head>
<title>Vue.js Computed Property</title>
<link rel="stylesheet" href="index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="eg_2">{{ fullName }}</div>
<script src="index.js"></script>
</body>
</html>
var vm = new Vue({
el: '#eg_2',
data: {
firstName: 'Alex',
lastName: 'Panda'
},
computed: {
fullName: function () {
return this.firstName + ' ' + this.lastName
}
}
})