加载登录页面时-我想检查是否设置了GET变量,并在我的Vue应用程序中获取它们(这样我就可以更改显示的模式)。
我的代码如下:
let routes = [
{
path: '/login',
component: Login
},
{
path: '/login/:user/:token',
component: Login,
// props: true
props: ['user', 'token']
},
...
];
...
...
...
export default {
props: [ 'user', 'token' ],
data() {
return {
// Change to yours for testing, empty these before production release
user: '',
pass: '',
getData: null
};
},
beforeRouteEnter (to, from, next) {
getPost(to.params.id, (err, post) => {
next(vm => vm.setGetData(err, post))
})
},
// When route changes and this component is already rendered,
// the logic will be slightly different.
beforeRouteUpdate (to, from, next) {
this.getData = null
getPost(to.params.id, (err, post) => {
this.setGetData(err, post)
next()
})
},
methods: {
setGetData (err, post) {
if (err) {
this.error = err.toString()
} else {
this.getData = post
}
}
}
}
getPost is not defined
). 我对Vue/VueRouter(我使用的是vanilla js或php)还不太熟悉,我的问题可能看起来很基本,但说真的,还没有找到任何能代表完整解释的获取get数据示例的东西。
如何获取GET变量,
之前
我的网址看起来对吗?
https://localhost:3000/login?user=potato&token=666
还是应该这样?
https://localhost:3000/login/potato/666
手动处理时
window.location.href
正如建议打破我的代码:
beforeRouteEnter(to, from, next) {
console.log(window.location.href);
if (this.getGetVariables(window.location.href)){
console.log('has token: ' + this.token );
}
next()
},
methods: {
// URL Variables:
getGetVariables: function(urlString) {
var url = new URL(urlString);
this.token = url.searchParams.get('token');
console.log(this.token);
if (!this.token) return false;
this.user = url.searchParams.get('user');
console.log(this.user);
}
类型错误:无法读取未定义的属性“getGetVariables”
似乎在它到达之前不会设置变量
create()
(我想)。那么我应该在哪里保存URL变量呢?
我试着改变
beforeRouteEnter
姓名
beforeRouteUpdate
在那个函数中,它不再正确地获取URL。