如何将用户输入从角度组件传递到快递服务器“获取路由器”并返回数据? 这是我到目前为止的代码,它返回未定义的内容:
<input type="text" (keyup)="onNameKeyUp($event)">
<button (click)="getWords()">Get Profile</button>
<span>{{name}}'s translation is {{translation}}</span>
我定义的component.ts中的:
name: string;
onNameKeyUp(event: any){
this.name= event.target.value;
}
searchParams={
params:{
param1: this.name
}
}
getWords(){
return this.httpClient.get('http://localhost:3000/words/',
this.searchParams)
.subscribe(
(data: any[])=>{
if(data.length){
console.log(data[0].translation);
}
}
)
}
在快递服务器中:
app.get('/words', (req, res)=>{
var val2= req.query.param1;
res.json(val2);
});
快递服务器中的数据是一个单词数组:
var words=[
{spelling: "aman", category: "noun", translation: "water"},
{spelling: "azzel", category: "verb", translation: "run"},
{spelling: "aberkan", category: "adjective", translation: "black"},
{spelling: "gar", category: "preposition", translation: "between"}
];
此刻,我想知道如何将用户输入服务器,然后再从单词数组中返回数据,例如单词的翻译。