mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-27 08:02:01 +00:00
Update the use of redirect(status, path, query)
This commit is contained in:
parent
78aabe480f
commit
4b0b83b782
@ -25,19 +25,17 @@ export default context => {
|
|||||||
context.nuxt = { data: [], error: null<%= (store ? ', state: null' : '') %>, serverRendered: true }
|
context.nuxt = { data: [], error: null<%= (store ? ', state: null' : '') %>, serverRendered: true }
|
||||||
// create context.next for simulate next() of beforeEach() when wanted to redirect
|
// create context.next for simulate next() of beforeEach() when wanted to redirect
|
||||||
context.redirected = false
|
context.redirected = false
|
||||||
context.next = function (path) {
|
context.next = function (opts) {
|
||||||
if (!path) return;
|
|
||||||
if (typeof path === 'string') path = { path }
|
|
||||||
path.query = stringify(path.query || {})
|
|
||||||
path.path = path.path + (path.query ? '?' + path.query : '')
|
|
||||||
path.path = urlJoin('<%= router.base %>', path.path)
|
|
||||||
if (!context.res) {
|
if (!context.res) {
|
||||||
context.redirected = path.path
|
context.redirected = opts
|
||||||
context.nuxt.serverRendered = false
|
context.nuxt.serverRendered = false
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
context.res.writeHead(302, {
|
opts.query = stringify(opts.query)
|
||||||
'Location': path.path
|
opts.path = opts.path + (opts.query ? '?' + opts.query : '')
|
||||||
|
opts.path = urlJoin('<%= router.base %>', opts.path)
|
||||||
|
context.res.writeHead(opts.status, {
|
||||||
|
'Location': opts.path
|
||||||
})
|
})
|
||||||
context.res.end()
|
context.res.end()
|
||||||
}
|
}
|
||||||
|
@ -25,9 +25,19 @@ export function getContext (context) {
|
|||||||
}
|
}
|
||||||
ctx.params = ctx.route.params || {}
|
ctx.params = ctx.route.params || {}
|
||||||
ctx.query = ctx.route.query || {}
|
ctx.query = ctx.route.query || {}
|
||||||
ctx.redirect = function (path) {
|
ctx.redirect = function (status, path, query) {
|
||||||
if (!path) return
|
if (!status) return
|
||||||
context.next(path)
|
// if only 1 or 2 arguments: redirect('/') or redirect('/', { foo: 'bar' })
|
||||||
|
if (typeof status === 'string' && (typeof path === 'undefined' || typeof path === 'object')) {
|
||||||
|
query = path || {}
|
||||||
|
path = status
|
||||||
|
status = 302
|
||||||
|
}
|
||||||
|
context.next({
|
||||||
|
path: path,
|
||||||
|
query: query,
|
||||||
|
status: status
|
||||||
|
})
|
||||||
}
|
}
|
||||||
if (context.req) ctx.req = context.req
|
if (context.req) ctx.req = context.req
|
||||||
if (context.res) ctx.res = context.res
|
if (context.res) ctx.res = context.res
|
||||||
|
Loading…
Reference in New Issue
Block a user