我在我的 ng 目录中构建了一个新的 js 文件 user.svc.js ,该文件的内容是
angular.module('app')
.service('UserSvc',function($http) {
var svc = this;
svc.getUser = function () {
return $http.get('http://localhost:3000/api/users',{
headers:{'X-Auth : this.token'}
})
}
svc.login = function(username,password){
return $http.post('http://localhost:3000/api/sessions',{
username:username,password:password
}).then(function(val){
svc.token = val.data;
return svc.getUser();
})
}
})
我收到的错误是:
C:\Users\setCodesToFire\Documents\modernweb\app>gulp js
[00:57:06] Using gulpfile ~\Documents\modernweb\app\gulpfile.js
[00:57:06] Starting 'js'...
[00:57:06] Finished 'js' after 79 msevents.js:160
throw er; // Unhandled 'error' event
^
Error: app.js: error: couldn't process source due to parse error
Unexpected token (54:2)
谁能告诉我问题是什么。它必须对 $http 做些什么吗?
最佳答案
将 header 值更改为对象格式而不是字符串值
改变这个
headers:{'X-Auth : this.token'}
到此
headers: {
'X-Auth' : 'this.token'
}
关于javascript - 当我更改 js 文件时 gulp js 给出了错误..| throw 者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45686576/