【发布时间】:2018-09-04 14:03:25
【问题描述】:
我正在使用 Vue Router 构建一个应用程序,该应用程序使用“外部”站点获取登录凭据。它基本上看起来像这样:
Home.Vue
<template>
</template>
<script>
export default {
name: "Home",
data() {
return {
}
},
created() {
window.location = 'https://third_party_site.com?nextpage=http://my_site.com/#/entry'
}
}
</script>
<style scoped>
</style>
此third_party_site.com 处理身份验证并将JWT 作为结果URL 中的查询字符串发回。当我尝试使用导航守卫(如下所示)获取查询字符串时,Vue 路由器只查看路由器的历史记录。
router/index.js
import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/components/Home'
import Entry from '@/components/Entry'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
name: 'Home',
component: Home,
},
{
path: '/entry',
name: 'Entry',
component: Entry,
beforeEnter: (to, from, next) => {
console.log('to',to)
console.log('from',from)
console.log('next',next)
next()
},
meta: {
title: 'Entry'
}
},
]
})
控制台打印预期的“to”位置,但“from”位置是基本路径“/”,并且没有查询参数。如何获取“外部”路由以便访问查询参数?生成的“来自”的 console.log 如下。
from
{name: null, meta: {…}, path: "/", hash: "", query: {…}, …}
fullPath: "/"
hash: ""
matched: []
meta: {}
name: null
params: {}
path: "/"
query: {}
__proto__: Object
在询问之前,必须以这种方式构建处于当前状态的应用程序,并且我无权访问外部站点。
【问题讨论】:
-
您是说远程站点在返回您的站点时没有向您的
/entryURL 添加查询参数? -
我注意到您没有在路由器中使用 HTML5 历史记录模式,所以
nextpageURL 不应该类似于http://my_site.com/#/entry吗? -
这正是我所说的,Phil。无论是在 /entry 之后添加还是在导航守卫的“from”中可用都无关紧要。我只需要在某个时间点访问它。您对哈希的评论也是正确的;这是我格式化问题时的拼写错误,但它包含在 Home.vue 的 window.location url 中
-
使用浏览器的 Network 控制台并选中 "Preserve log" 选项。在您浏览身份验证时遵循请求并查看是否...a)远程站点是否包含正确的查询参数,以及 b)如果是,它会发生什么
标签: vue.js jwt query-string