nuxt-interpolation
Advanced tools
| const { resolve } = require('path') | ||
| module.exports = function interpolation () { | ||
| this.addPlugin(path.resolve(__dirname, 'templates/plugin.js')) | ||
| } | ||
| // REQUIRED if publishing as an npm package | ||
| module.exports.meta = require('./package.json') |
| import Vue from 'vue' | ||
| export default ({ app: { router } }) => { | ||
| // https://vuejs.org/v2/guide/custom-directive.html | ||
| Vue.directive('interpolation', { | ||
| // Called only once, when the directive is first bound to the element. This is where you can do one-time setup work. | ||
| bind (el) { | ||
| const navigate = event => { | ||
| const href = event.target.getAttribute('href') | ||
| if (href && href[0] === '/') { | ||
| event.preventDefault() | ||
| router.push(href) | ||
| } | ||
| } | ||
| let links = el.getElementsByTagName('a') | ||
| const addListeners = links => { | ||
| for (let i = 0; i < links.length; i++) { | ||
| const target = links[i].getAttribute('target') | ||
| // For improved security `rel="noopener"` will be added automatically if target is `_blank` | ||
| // https://github.com/mathiasbynens/rel-noopener/ | ||
| if (target && target === '_blank') { | ||
| links[i].setAttribute('rel', 'noopener') | ||
| } | ||
| links[i].addEventListener('click', navigate, false) | ||
| } | ||
| } | ||
| const removeListeners = links => { | ||
| for (let i = 0; i < links.length; i++) { | ||
| links[i].removeEventListener('click', navigate, false) | ||
| } | ||
| links = [] | ||
| } | ||
| addListeners(links) | ||
| el.$componentUpdated = () => { | ||
| removeListeners(links) | ||
| Vue.nextTick(() => addListeners(links)) | ||
| } | ||
| el.$destroy = () => el.removeEventListener('click', removeListeners(links)) | ||
| }, | ||
| // Called after the containing component’s VNode and the VNodes of its children have updated. | ||
| componentUpdated: el => el.$componentUpdated(), | ||
| // Called only once, when the directive is unbound from the element. | ||
| unbind: el => el.$destroy() | ||
| }) | ||
| } |
+10
-0
@@ -5,2 +5,12 @@ # Change Log | ||
| <a name="0.2.0"></a> | ||
| # [0.2.0](https://github.com/daliborgogic/nuxt-interpolation/compare/v0.1.7...v0.2.0) (2018-09-13) | ||
| ### Features | ||
| * we are now a module ([c1fb01e](https://github.com/daliborgogic/nuxt-interpolation/commit/c1fb01e)) | ||
| <a name="0.1.7"></a> | ||
@@ -7,0 +17,0 @@ ## [0.1.7](https://github.com/daliborgogic/nuxt-interpolation/compare/v0.1.6...v0.1.7) (2018-09-13) |
+1
-1
| { | ||
| "name": "nuxt-interpolation", | ||
| "version": "0.1.7", | ||
| "version": "0.2.0", | ||
| "description": "Directive for binding every link to catch the click event, and if it's a relative link router will push.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+5
-3
| # interpolation | ||
| > [Nuxt.js](https://github.com/nuxt/nuxt.js/) directive for binding every link to catch the click event, and if it's a relative link router will push. | ||
| > [Nuxt.js](https://github.com/nuxt/nuxt.js/) directive for binding every link to catch the click event, and if it's a relative link router will push. For improved security `rel="noopener"` will be added automatically if target is `_blank` | ||
@@ -8,7 +8,9 @@ ## Setup | ||
| - Add ```nuxt-interpolation``` as dependency | ||
| - Point ```nuxt-interpolation``` to plugins section of ```nuxt.config.js``` | ||
| - Add ```nuxt-interpolation``` to plugins section of ```nuxt.config.js``` | ||
| ```javascript | ||
| { | ||
| plugins: ['./node_modules/nuxt-interpolation'] | ||
| modules: [ | ||
| ['nuxt-interpolation'] | ||
| ] | ||
| } | ||
@@ -15,0 +17,0 @@ ``` |
-54
| import Vue from 'vue' | ||
| export default ({ app: { router } }) => { | ||
| // https://vuejs.org/v2/guide/custom-directive.html | ||
| Vue.directive('interpolation', { | ||
| // Called only once, when the directive is first bound to the element. This is where you can do one-time setup work. | ||
| bind (el) { | ||
| const navigate = event => { | ||
| const href = event.target.getAttribute('href') | ||
| if (href && href[0] === '/') { | ||
| event.preventDefault() | ||
| router.push(href) | ||
| } | ||
| } | ||
| let links = el.getElementsByTagName('a') | ||
| const addListeners = links => { | ||
| for (let i = 0; i < links.length; i++) { | ||
| const target = links[i].getAttribute('target') | ||
| // For improved security `rel="noopener"` will be added automatically if target is `_blank` | ||
| // https://github.com/mathiasbynens/rel-noopener/ | ||
| if (target && target === '_blank') { | ||
| links[i].setAttribute('rel', 'noopener') | ||
| } | ||
| links[i].addEventListener('click', navigate, false) | ||
| } | ||
| } | ||
| const removeListeners = links => { | ||
| for (let i = 0; i < links.length; i++) { | ||
| links[i].removeEventListener('click', navigate, false) | ||
| } | ||
| links = [] | ||
| } | ||
| addListeners(links) | ||
| el.$componentUpdated = () => { | ||
| removeListeners(links) | ||
| Vue.nextTick(() => addListeners(links)) | ||
| } | ||
| el.$destroy = () => el.removeEventListener('click', removeListeners(links)) | ||
| }, | ||
| // Called after the containing component’s VNode and the VNodes of its children have updated. | ||
| componentUpdated: el => el.$componentUpdated(), | ||
| // Called only once, when the directive is unbound from the element. | ||
| unbind: el => el.$destroy() | ||
| }) | ||
| } |
5557
11.3%7
16.67%50
13.64%29
7.41%