我将 AngularJS 与 ES6 结合使用,当我从服务中调用 register 函数时,出现错误:
this.$http is undefined
我的服务:
export default class AccountService {
/* @ngInject */
contructor($http) {
this.$http = $http;
}
register(account) {
return this.$http.post('/api/accounts/register', account).then(result => result.data);
}
}
AccountService.$inject = ['$http'];
注册我的服务:
import AccountService from './services/accounts.service';
angular.module('app', [...])
/* ... */
.service('accountService', AccountService)
在我的 Controller 中:
export class RegisterController {
/* @ngInject */
constructor(accountService) {
this.accountService = accountService;
}
register(account) {
this.accountService.register(account);
}
}
最佳答案
您将constructor
拼写为constructor
。它永远不会被调用,因此不会分配 this.$http
。
关于javascript - Angular 1.x ES6 服务 $http 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34329804/