From 3396a5af7d7e91c90f6873bfbf634051e5ea2e2c Mon Sep 17 00:00:00 2001 From: nikolaynesov Date: Tue, 4 Jul 2017 15:40:22 +0000 Subject: [PATCH] Use route.matched insted of full path to detect route relations --- index.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 31c09a5..539492e 100644 --- a/index.js +++ b/index.js @@ -10,23 +10,30 @@ class VueBodyClassController { router.beforeEach((to, from, next) => { var parent = router.options.routes; - let pathSplitted = to.path.split('/'); + let matched = []; var additionalClassName = ""; - let goDeep; - pathSplitted = pathSplitted.filter(function(n){ return (n) }); - goDeep = (to.path != '/' && to.path.length > 0 && pathSplitted.length > 0); + for (let index in to.matched) { - if (goDeep) { + let prev = matched.join('/'); - for (var prop in pathSplitted) { + matched.push(to.matched[index].path + .replace(/^\/|\/$/g, '') + .replace(prev, '') + .replace(/^\/|\/$/g, '')); + + } + + if (to.path != '/' && to.path.length > 0 && matched.length > 0) { + + for (let index in matched) { let data = parent.children ? parent.children : parent; let found; found = data.find((o)=> { - return o.path == '/' + pathSplitted[prop] || o.path == pathSplitted[prop]; + return o.path.replace(/^\/|\/$/g, '') == matched[index]; });