image.png

代码

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vue.js 监听属性-千米与米之间的换算</title>
    <script src="//static.gzj2001.com/vue/vue.js"></script>
</head>
<body>
    <div id="app">
        米<input type="text"  v-model="mi">
        千米<input type="text"  v-model="km">
    </div>
    <script>
        new Vue({
            el:"#app",
            data:{
                mi:0,
                km:0
            },
            watch:{
                km:function(val){
                    this.km = val;
                    this.mi = val * 1000;
                },
                mi:function(cs){
                    this.mi = cs;
                    this.km = cs / 1000;
                }

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