<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>自定义过滤器</title>
	<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
	<script>
		/**
		 * 自定义全局过滤器
		 */
		Vue.filter('addZero',function(data){
            console.log(data);
            return data<10?'0'+data:data;
          
        })

		window.onload=function(){
			let vm=new Vue({
				el:'#app',
				data:{
                    currentTime:Date.now()
				},
				//局部过滤器
				filters:{ 
					number:(data,n) => {
						return data.toFixed(n);
					},
					dateformat(data){
						var d = new Date(data);
            			return d.getFullYear()+'-'+(d.getMonth()+1)+'-'+d.getDate()+' '+d.getHours()+':'+d.getMinutes()+':'+d.getSeconds();
					}
				}
			});
		}
	</script>
</head>
<body>
	<div id="app">
		<h3>{{3 | addZero}}</h3>
		<h3>{{12.345678 | number(2)}}</h3>
		<h3>{{currentTime | dateformat}}</h3>
	</div>
</body>
</html>
Logo

前往低代码交流专区

更多推荐