我正在尝试在我的 WinJS Windows 8 应用程序中实现一个服务,该服务需要调用 winjs httpClient。我希望我的服务在等待 httpClient 返回的 promise 时返回 promise 。我的服务代码如下
(function () {
"use strict";
var _httpClient = new Windows.Web.Http.HttpClient();
var _infoUri = new Windows.Foundation.Uri("https://news.google.com/");
var _getLatestInfo = function () {
return WinJS.Promise(function (completeCallback, errorCallback, progressCallback) {
// invoke the httpclient here
_httpClient.getAsync(_infoUri)
.then(function complete(result) {
completeCallback(result);
}, function error(result) {
errorCallback(result);
}, function progress(result) {
progressCallback(result);
});
});
};
WinJS.Namespace.define("DataService", {
getLatestInfo: _getLatestInfo
});
})();
我按如下方式调用我的服务方法
(function () {
"use strict";
WinJS.UI.Pages.define("/pages/home/home.html", {
// This function is called whenever a user navigates to this page. It
// populates the page elements with the app's data.
ready: function (element, options) {
// TODO: Initialize the page here.
DataService.getLatestInfo().then(function () { }, function () { }, function () { });
}
});
})();
这不起作用,我收到这样的错误
Exception was thrown at line 2018, column 13 in ms-appx://microsoft.winjs.2.0/js/base.js
0x800a01b6 - JavaScript runtime error: Object doesn't support property or method '_setState''
我尝试按如下方式简化我的服务,但没有成功。
(function () {
"use strict";
var _getLatestInfo = function () {
return WinJS.Promise(function (a, b, c) { });
};
WinJS.Namespace.define("DataService", {
getLatestInfo: _getLatestInfo
});
})();
我不知道该错误试图告诉我什么以及如何纠正它。
最佳答案
我刚刚发现,我错过了 WinJS.Promise 之前的"new"。由于 WinJS.Promise 是一个类,因此显然需要更新。错误消息只会让我更加困惑。
关于javascript - WinJS Promise 导致 base.js 中的 JavaScript 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24582743/