|
| 1 | +# Plugins & Servicios |
| 2 | +```js |
| 3 | +MyPlugin.install = function (Vue, options) { |
| 4 | + // 1. agregar método global o propiedad |
| 5 | + Vue.myGlobalMethod = function () { |
| 6 | + // algo de lógica... |
| 7 | + } |
| 8 | + |
| 9 | + // 2. agregar un recurso global |
| 10 | + Vue.directive('my-directive', { |
| 11 | + bind (el, binding, vnode, oldVnode) { |
| 12 | + // algo de lógica... |
| 13 | + } |
| 14 | + ... |
| 15 | + }) |
| 16 | + |
| 17 | + // 3. inyectar algunas opciones de componentes |
| 18 | + Vue.mixin({ |
| 19 | + created: function () { |
| 20 | + // algo de lógica... |
| 21 | + } |
| 22 | + ... |
| 23 | + }) |
| 24 | + |
| 25 | + // 4. agregar un método de instancia |
| 26 | + Vue.prototype.$myMethod = function (methodOptions) { |
| 27 | + // algo de lógica... |
| 28 | + } |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +# Filtros |
| 33 | +```js |
| 34 | +const msToMm = {} |
| 35 | + |
| 36 | +function convertMsToMm (ms) { |
| 37 | + const min = Math.floor(ms / 60000) |
| 38 | + const sec = ((ms % 60000) / 1000).toFixed(0) |
| 39 | + |
| 40 | + return `${min}:${sec < 10 ? `00` : sec} min` |
| 41 | +} |
| 42 | + |
| 43 | +msToMm.install = function (Vue) { |
| 44 | + Vue.filter('msToMm', val => { |
| 45 | + return convertMsToMm(val) |
| 46 | + }) |
| 47 | +} |
| 48 | + |
| 49 | +export default msToMm |
| 50 | + |
| 51 | +``` |
| 52 | + |
| 53 | +# Mixins |
| 54 | +```js |
| 55 | +const myOwnMixin = { |
| 56 | + data() { |
| 57 | + return {} |
| 58 | + }, |
| 59 | + methods: {}, |
| 60 | + computed: {} |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +export default myOwnMixin |
| 65 | +``` |
| 66 | + |
| 67 | + |
| 68 | +# Directivas |
| 69 | +```js |
| 70 | +const blur = {} |
| 71 | + |
| 72 | +function setBlur (el, binding) { |
| 73 | + el.style.filter = !binding.value ? 'blur(3px)' : '(none)' |
| 74 | + el.style.filter = !binding.value ? 'not-allowed' : 'inherit' |
| 75 | + |
| 76 | + el.querySelectorAll('a').forEach(element => { |
| 77 | + if (!binding.value) { |
| 78 | + element.setAttribute('disabled', true) |
| 79 | + } else { |
| 80 | + element.removeAttribute('disabled') |
| 81 | + } |
| 82 | + }) |
| 83 | +} |
| 84 | + |
| 85 | +blur.install = function (Vue) { |
| 86 | + Vue.directive('blur', { |
| 87 | + bind (el, binding) { |
| 88 | + setBlur(el, binding) |
| 89 | + } |
| 90 | + }) |
| 91 | +} |
| 92 | + |
| 93 | +export default blur |
| 94 | +``` |
| 95 | + |
| 96 | + |
| 97 | +# Watchers |
| 98 | +```js |
| 99 | +export default { |
| 100 | + data() { |
| 101 | + return { |
| 102 | + value: 0 |
| 103 | + } |
| 104 | + }, |
| 105 | + watch: { |
| 106 | + value(val) { |
| 107 | + console.log('algo ha cambiado!!!') |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
0 commit comments