You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					60 lines
				
				1.8 KiB
			
		
		
			
		
	
	
					60 lines
				
				1.8 KiB
			| 
											3 years ago
										 | <!DOCTYPE html>
 | ||
|  | <html lang="en">
 | ||
|  | <head>
 | ||
|  |     <meta charset="UTF-8">
 | ||
|  |     <title>过滤器</title>
 | ||
|  |     <script type="text/javascript" src="../vue.js"></script>
 | ||
|  |     <script src="https://cdn.bootcdn.net/ajax/libs/dayjs/1.11.7/dayjs.min.js"></script>
 | ||
|  | </head>
 | ||
|  | <body>
 | ||
|  | <div id="root">
 | ||
|  |     <h2>显示格式化后的时间</h2>
 | ||
|  |     <!--    计算属性实现-->
 | ||
|  |     <h3>计算属性实现:{{formatDatetime}}</h3>
 | ||
|  |     <!--    方法实现-->
 | ||
|  |     <h3>方法实现:{{fmtDatetime()}}</h3>
 | ||
|  |     <!--    过滤器实现-->
 | ||
|  |     <h3>过滤器实现1:{{time | formatDt}}</h3>
 | ||
|  |     <!--    过滤器传参-->
 | ||
|  |     <h3>过滤器实现2:{{time | formatDt('YYYY_MM_DD')}}</h3>
 | ||
|  |     <!--    多过滤器-->
 | ||
|  |     <h3>多过滤器实现:{{time | formatDt('YYYY_MM_DD') | yearSlice}}</h3>
 | ||
|  |     <!--    全局过滤器-->
 | ||
|  |     <h3>全局过滤器:{{time | formatDt('YYYY_MM_DD') | globalSlice}}</h3>
 | ||
|  |     <!--    绑定时使用过滤器-->
 | ||
|  |     <h3 :t="time | formatDt">绑定时使用过滤器:F12</h3>
 | ||
|  | </div>
 | ||
|  | 
 | ||
|  | </body>
 | ||
|  | <script type="text/javascript">
 | ||
|  |     Vue.config.productionTip = false
 | ||
|  |     // 全局过滤器
 | ||
|  |     Vue.filter('globalSlice', function (val) {
 | ||
|  |         return val.slice(0, 4)
 | ||
|  |     })
 | ||
|  |     const vm = new Vue({
 | ||
|  |         el: '#root',
 | ||
|  |         data: {
 | ||
|  |             time: Date.now()
 | ||
|  |         },
 | ||
|  |         methods: {
 | ||
|  |             fmtDatetime() {
 | ||
|  |                 return dayjs(this.time).format("YYYY年MM月DD日 HH:mm:ss")
 | ||
|  |             }
 | ||
|  |         },
 | ||
|  |         computed: {
 | ||
|  |             formatDatetime() {
 | ||
|  |                 return dayjs(this.time).format("YYYY-MM-DD HH:mm:ss")
 | ||
|  |             }
 | ||
|  |         },
 | ||
|  |         filters: {
 | ||
|  |             formatDt(val, fmt_str = "YYYY_MM_DD HH:mm:ss") {
 | ||
|  |                 return dayjs(val).format(fmt_str)
 | ||
|  |             },
 | ||
|  |             yearSlice(val) {
 | ||
|  |                 return val.slice(0, 4)
 | ||
|  |             }
 | ||
|  |         }
 | ||
|  |     })
 | ||
|  | </script>
 | ||
|  | </html>
 |