gpt4 book ai didi

javascript - Angular 2 : Error during evaluation of click

转载 作者:行者123 更新时间:2023-11-29 21:33:59 24 4
gpt4 key购买 nike

我通过使用 express 创建的从客户端到服务器的按钮发出请求,在请求处理程序中只有 console.log('Delete from server');每次我点击它都会收到这些错误

angular2.dev.js:23514 ORIGINAL EXCEPTION: T
ypeError: Cannot read property 'request' of undefined
angular2-polyfills.js:143 Uncaught EXCEPTION:
Error during evaluation of "click"
ORIGINAL EXCEPTION: TypeError: Cannot read property 'request' of undefined
ORIGINAL STACKTRACE:

文件结构是,有两个文件夹server和client,分别有angular和server文件

这里是点击按钮的函数:

 deletefromserver(){
this.http.request("http://localhost:3000/deletedata").subscribe((res : Response) => {
console.log(res.json());
})
}

这是服务器文件中的请求处理程序:

server.get('/deletedata', function(){
console.log('Delete from server');
})

最佳答案

你可能忘了在你的组件中注入(inject) http 服务

你应该有这样的东西:

@Component({...})
class MyComponent {

constructor(private http: Http) { }

deleteFromServer() {
this.http.request("http://localhost:3000/deletedata").subscribe((res : Response) => { console.log(res.json()); })
}

}

并且不要忘记在您的 Bootstrap 中添加 HTTP_PROVIDERS:

bootstrap(MyComponent, [HTTP_PROVIDERS]);

你的服务器也应该返回一些东西:

server.get('/deletedata', function(req, res) {
res.json({hello: 'hello world'});
});

关于javascript - Angular 2 : Error during evaluation of click,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35316270/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com