自定义指令 格式化input数据为非负整数

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
<div id="app">
    <input type="text" v-model="available_time" v-format-floor>
    <input type="text" v-model="a" v-format-floor>
    <button @click="show">button</button>
</div>
    <script>
        var vm = new Vue({
            el:"#app",
            data:{
                available_time:"",
                a: '',
            },
            methods:{
                show(){
                    console.log(this.a)
                }
            },
            directives: {
                
                "format-floor": {
                    update: function (e,vnode,oldVnode) {
                        // 获取变量名字
                        var temp = oldVnode.data.directives[0].expression;var e = e.value;
                        e = Math.floor(e) || 0;
                        vm[temp] = e;
                    }
              }
            }
        })
    </script>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/hill-foryou/p/9208587.html