在methods中使用filter

<!DOCTYPE html>
<html lang="zh">
<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>在methods中使用filter过滤器中的内容</title>
</head>
<body>
	<div id="app">
		<!--全局过滤器-->
		<button @click="but">全局过滤器</button>
		<!--私有过滤器-->
		<button @click="btn"> 私有过滤器 </button>
	</div>
	<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
	<script>
		//定义全局filter过滤器
		Vue.filter('nums',function(val){
			return val >= 10 ? val : '0'+val
		})
		var vm = new Vue({
			el:'#app',
			methods:{
				but(){
					console.log(Vue.filter('nums')(1)) // 01
				},
				//私有过滤器
				btn(){
					//console.log(this.$options.filters) //得到一个数组
					console.log(this.$options.filters['num'](3)) // 03
					//使用
					var text = this.$options.filters['num']
					console.log(text(3)) // 03
				}
			},
			//私有过滤器
			filters:{
				num(val){
					return val >= 10 ? val : '0'+val
				}
			},
			
		})
	</script>
</body>
</html>

posted on 2019-05-08 16:20  A-pupli  阅读(233)  评论(0编辑  收藏  举报