vue2.0

vue自定义指令,过滤器等

2021-10-13  本文已影响0人  zlf_j

1、vue 去除前后的空格

<el-input v-model.trim="data"></el-input>

https://www.cnblogs.com/mmzuo-798/p/9301109.html

2、vue自定义指令

Vue.directive('bgcolor', function (el, binding) {
      el.style.backgroundColor = binding.value
})
directives: {
    bgcolor: (el, binding) => {
        el.style.backgroundColor = binding.value  
    }
}

https://www.cnblogs.com/dhui/p/13268040.html

3、vue过滤器,filters

filters: {
    componentFilter(value) {
      return value + '!!!'
    },
  },
用法:{{ 'a' | componentFilter }} // a!!!

https://www.jianshu.com/p/ad21df1914c5

方法1

Vue.filter('aaa', function (value) {
    return value + '!!!'
})

https://www.jianshu.com/p/ad21df1914c5

方法2

const isNullOrEmpty = function(val) {
    if (val == null || val == "" || typeof(val) == undefined) {
        return true;
    } else {
        return false;
    }
}
export {
    isNullOrEmpty
}
import * as filters from '../filters/index'
Object.keys(filters).forEach(key => {
    Vue.filter(key, filters[key])
})
{{date | isNullOrEmpty}}是否为空

https://www.cnblogs.com/yanwuming/p/10603058.html

4、指令了解

https://jspang.com/detailed?id=21#toc330

<div v-pre>{{message}}</div> // 直接显示{{message}}
[v-cloak] {
  display: none;
}
<div v-cloak>
  {{ message }}
</div>
<div v-once>{{info}}</div

5、组件全局注册

import Vue from 'vue'
import pageHead from './components/page-head.vue'
Vue.component('page-head',pageHead)
<template>
    <div>
         <page-head></page-head>
    </div>
</template>
上一篇 下一篇

猜你喜欢

热点阅读