【发布时间】:2017-04-04 01:19:10
【问题描述】:
我需要帮助才能从一个 aurelia 页面获取数据到另一个页面。 我在登录页面询问用户名和密码。然后我检查这个用户是否在数据库中。问题是,我不知道如何记住下一页中的用户名。我需要这里的用户名:
import {HttpClient, json} from 'aurelia-fetch-client'
export class User{
constructor(userData){
this.username= userData.username;
}
userData = {}
userList= []
getUser(x, y){
console.log(x)
this.username = x;
console.log(y)
let client = new HttpClient();
client.fetch('http://localhost:8080/users/'+ x)
.then(response => response.json())
.then(data => {
if(JSON.stringify(data.password) === '"'+y+'"'){
console.log("Okei")
var landingUrl = "http://" + window.location.host + "#/main";
window.location.href = landingUrl
}else{
console.log("Wrong password")
alert("Wrong password! Try again")
}
})
}
}
到那个页面:
import {HttpClient, json} from 'aurelia-fetch-client'
import { inject } from 'aurelia-framework'
import { User } from '../home/index';
@inject(User)
export class Diet{
constructor(userData) {
this.username = userData.username;
}
somethingSpecial() {
console.log(this.username);
}
dietData = {}
dietList=[]
addDiet(){
let client = new HttpClient();
client.fetch('http://localhost:8080/diet/add', {
'method': "POST",
'body': json(this.dietData)
})
.then(response => response.json())
.then(data => {
console.log("Server sent " + data.foodName);
});
document.getElementById("form").reset();
this.somethingSpecial();
}
}
我尝试过使用构造函数,但现在它说 userData 是未定义的,所以它没有从第一页获取信息。我做错了什么? 谢谢!
【问题讨论】:
-
看看将您的用户会话数据存储在本地存储中 - 这应该可以解决您的问题。
-
或者,看看这篇关于共享状态的文章 - ilikekillnerds.com/2016/02/shared-state-in-aurelia
标签: javascript aurelia