This time I will bring you the custom directive directive of Vue.js. What are the precautions for using the custom directive directive of Vue.js? The following is a practical case, let’s take a look.
Take custom v-css instructions as an example:
Local instructions
<script> export default { //自定义v-css指令 directives: { css: { inserted (el, bind) { let styleObj = bind.value let arr = [] for (let key in styleObj) { arr.push(key + ':' + styleObj[key]) } arr = arr.join(';') el.style.cssText = arr } } }</script>
Global instructions
Customize global instructions in the main.js file
Vue.directive('css', { inserted (el, binding) { let styleObj = binding.value let arr = [] for (let key in styleObj) { arr.push(key + ':' + styleObj[key]) } arr = arr.join(';') el.style.cssText = arr } })
Use custom instructions
<p v-css="{color: 'orange', 'font-size': '24px'}">我是p标签</p>
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Recommended reading:
Computed properties and data monitoring of Vue.js
Vue.js events Binding - built-in event binding, custom event binding
Vue.js list rendering v-for array object subcomponent
The above is the detailed content of Vue.js custom directive directive. For more information, please follow other related articles on the PHP Chinese website!