Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove route #2935

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions examples/remove-routes/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const Foo = { template: '<div>foo</div>' }
const Bar = { template: '<div>bar</div>' }

const router = new VueRouter({
mode: 'history',
base: __dirname,
routes: [
{ path: '/foo', component: Foo }
]
})

new Vue({
router,
template: `
<div id="app">
<h1>Add and Remove Routes</h1>
<ul>
<li><router-link to="/foo">/foo</router-link></li>
<li><router-link to="/bar">/bar</router-link></li>
</ul>
<button id="add-btn" @click="addRouteBar">Add Route Bar</button>
<button id="remove-btn" @click="removeRouteBar">Remove Route Bar</button>
<router-view class="view"></router-view>
</div>
`,
methods: {
addRouteBar () {
this.$router.addRoutes([{ path: '/bar', component: Bar }])
},
removeRouteBar () {
this.$router.updateRoutes([{ path: '/bar' }])
}
}
}).$mount('#app')
6 changes: 6 additions & 0 deletions examples/remove-routes/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!DOCTYPE html>
<link rel="stylesheet" href="/global.css">
<a href="/">&larr; Examples index</a>
<div id="app"></div>
<script src="/__build__/shared.chunk.js"></script>
<script src="/__build__/remove-routes.js"></script>
9 changes: 7 additions & 2 deletions src/create-matcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import { resolvePath } from './util/path'
import { assert, warn } from './util/warn'
import { createRoute } from './util/route'
import { fillParams } from './util/params'
import { createRouteMap } from './create-route-map'
import { createRouteMap, removeRoutesFromMap } from './create-route-map'
import { normalizeLocation } from './util/location'

export type Matcher = {
match: (raw: RawLocation, current?: Route, redirectedFrom?: Location) => Route;
addRoutes: (routes: Array<RouteConfig>) => void;
removeRoutes: (routes: Array<RouteConfig>) => void;
};

export function createMatcher (
Expand All @@ -22,6 +23,9 @@ export function createMatcher (
function addRoutes (routes) {
createRouteMap(routes, pathList, pathMap, nameMap)
}
function removeRoutes (routes) {
removeRoutesFromMap(routes, pathList, pathMap, nameMap)
}

function match (
raw: RawLocation,
Expand Down Expand Up @@ -166,7 +170,8 @@ export function createMatcher (

return {
match,
addRoutes
addRoutes,
removeRoutes
}
}

Expand Down
75 changes: 75 additions & 0 deletions src/create-route-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,78 @@ function normalizePath (
if (parent == null) return path
return cleanPath(`${parent.path}/${path}`)
}

export function removeRoutesFromMap (
routes: Array<RouteConfig>,
pathList: Array<string>,
pathMap: Dictionary<RouteRecord>,
nameMap: Dictionary<RouteRecord>
) {
routes.forEach(route => {
removeRouteRecord(pathList, pathMap, nameMap, route)
})

// ensure wildcard routes are always at the end
for (let i = 0, l = pathList.length; i < l; i++) {
if (pathList[i] === '*') {
pathList.push(pathList.splice(i, 1)[0])
l--
i--
}
}
}

function removeRouteRecord (
pathList: Array<string>,
pathMap: Dictionary<RouteRecord>,
nameMap: Dictionary<RouteRecord>,
route: RouteConfig,
parent?: RouteRecord,
matchAs?: string
) {
const { path, name } = route
const pathToRegexpOptions: PathToRegexpOptions = route.pathToRegexpOptions || {}
const normalizedPath = normalizePath(
path,
parent,
pathToRegexpOptions.strict
)

if (route.children) {
route.children.forEach(child => {
const childMatchAs = matchAs
? cleanPath(`${matchAs}/${child.path}`)
: undefined
removeRouteRecord(pathList, pathMap, nameMap, child, { path: normalizedPath }, childMatchAs)
})
}

if (route.alias !== undefined) {
const aliases = Array.isArray(route.alias)
? route.alias
: [route.alias]

aliases.forEach(alias => {
const aliasRoute = {
path: alias,
children: route.children
}
removeRouteRecord(
pathList,
pathMap,
nameMap,
aliasRoute,
parent,
normalizedPath || '/' // matchAs
)
})
}
const index = pathList.indexOf(normalizedPath)
if (index > -1) {
pathList.splice(index, 1)
delete pathMap[normalizedPath]
if (name) {
delete nameMap[name]
}
}
}
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ export default class VueRouter {
this.history.transitionTo(this.history.getCurrentLocation())
}
}
removeRoutes (routes: Array<RouteConfig>) {
this.matcher.removeRoutes(routes)
if (this.history.current !== START) {
this.history.transitionTo(this.history.getCurrentLocation())
}
}
}

function registerHook (list: Array<any>, fn: Function): Function {
Expand Down
41 changes: 41 additions & 0 deletions test/e2e/specs/remove-routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const bsStatus = require('../browserstack-send-status')

module.exports = {
...bsStatus(),

'@tags': ['history'],

removeroutes: function (browser) {
browser
.url('http://localhost:8080/remove-routes/')
.waitForElementVisible('#app', 1000)
.assert.count('li', 2)
.assert.attributeContains('li:nth-child(1) a', 'href', '/remove-routes/foo')
.assert.attributeContains('li:nth-child(2) a', 'href', '/remove-routes/bar')
.assert.urlEquals('http://localhost:8080/remove-routes/')

.click('li:nth-child(2) a')
.assert.urlEquals('http://localhost:8080/remove-routes/bar')
.assert.elementNotPresent('.view')

.click('li:nth-child(1) a')
.assert.urlEquals('http://localhost:8080/remove-routes/foo')
.assert.containsText('.view', 'foo')

.click('#add-btn')
.click('li:nth-child(2) a')
.assert.urlEquals('http://localhost:8080/remove-routes/bar')
.assert.containsText('.view', 'bar')

.click('li:nth-child(1) a')
.assert.urlEquals('http://localhost:8080/remove-routes/foo')
.assert.containsText('.view', 'foo')

.click('#remove-btn')
.click('li:nth-child(2) a')
.assert.urlEquals('http://localhost:8080/remove-routes/bar')
.assert.containsText('.view', '')

.end()
}
}