<body><div id="app"><p v-color="'pink'" v-fontweight="900">{{ dt | msgFormat }}</p></div><script>//全局指令
Vue.directive('color',{
bind:function(el, binding){
el.style.color = binding.value;}})
var vm = new Vue({
el:"#app",
data:{
dt: new Date()},
methods:{},
filters:{
msgFormat:function(dateStr, pattern=''){
var dt = new Date(dateStr);
var y = dt.getFullYear();
var m =(dt.getMonth()+1).toString().padStart(2,'0');
var d = dt.getDate().toString().padStart(2,'0');if(pattern.toLowerCase()==='yyyy-mm-dd'){return `${y}-${m}-${d}`;}else{
var hh = dt.getHours().toString().padStart(2,'0');
var mm = dt.getMinutes().toString().padStart(2,'0');
var ss = dt.getSeconds().toString().padStart(2,'0');return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
}}},
directives:{//定义私有指令'fontweight':{
bind:function(el, binding){
el.style.fontWeight = binding.value;}}}});</script></body>
4.指令函数的简写形式
<body><div id="app"><p v-color="'pink'" v-fontweight="900" v-fontsize="'50'">{{ dt | msgFormat }}</p></div><script>//全局指令
Vue.directive('color',{
bind:function(el, binding){
el.style.color = binding.value;}})
var vm = new Vue({
el:"#app",
data:{
dt: new Date()},
methods:{},
filters:{
msgFormat:function(dateStr, pattern=''){
var dt = new Date(dateStr);
var y = dt.getFullYear();
var m =(dt.getMonth()+1).toString().padStart(2,'0');
var d = dt.getDate().toString().padStart(2,'0');if(pattern.toLowerCase()==='yyyy-mm-dd'){return `${y}-${m}-${d}`;}else{
var hh = dt.getHours().toString().padStart(2,'0');
var mm = dt.getMinutes().toString().padStart(2,'0');
var ss = dt.getSeconds().toString().padStart(2,'0');return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
}}},
directives:{//定义私有指令'fontweight':{
bind:function(el, binding){
el.style.fontWeight = binding.value;}},'fontsize':function(el, binding){//简写形式
el.style.fontSize =parseInt(binding.value)+'px';}}});</script></body>