Vue自定义指令

main.js

 

import Vue from "vue";
import App from "./App.vue";

Vue.directive("font", {
  bind(el, binding, vnode) {
    console.log(binding);
    el.style.fontFamily = binding.arg==='micro'?'Microsoft Yahei':'Arail';
    el.style.fontWeight = binding.modifiers.bold ? "bold" : "";
    el.style.fontStyle = binding.modifiers.italic ? "italic" : "";
    el.style.color = binding.value;
  }
});

new Vue({
  render: h => h(App)
}).$mount("#app");

 

App.vue

 

<template>
  <div id="app">
    <div v-font:micro.bold.italic="color">加勒比海盗</div>
  </div>
</template>

<script>
export default {
  name: "app",
  data: () => {
    return { color: "gold" };
  }
};
</script>

<style>
</style>

 

运行效果

 

 

posted on 2019-06-19 11:55  沙滩海风  阅读(317)  评论(0编辑  收藏  举报

导航