如何使用 javascript 获取 REST 客户端?

How do I get the REST client using javascript?

Visual Studios 在他们的网站上有以下打字稿代码,我试图将其作为示例:

import RestClient = require("TFS/WorkItemTracking/RestClient");
 // Get an instance of the client
var client = RestClient.getClient();
// Call a method on the client
// e.g. client.getResource(...).then(...);

我的 javascript 部分代码是:

VSS.init({
        explicitNotifyLoaded: true,
        usePlatformStyles: true
    });
  VSS.require([
      "VSS/Controls",
      "VSS/Controls/Menus",
      "VSS/Controls/Dialogs",
      "VSS/Service",
      "TFS/WorkItemTracking/RestClient",
      "VSS/WebApi/Contracts"],
      function (VSS_Service, TFS_Wit_WebApi) {
          //get the REST client
          var witClient = VSS_Service.getCollectionClient(TFS_Wit_WebApi.WorkItemTrackingHttpClient);
      }
  );

然而,当我这样做时,我收到一条错误消息,指出 VSS_Service.getCollectionClient(TFS_Wit_WebApi.WorkItemTrackingHttpClient) 不是一个函数。我是否需要初始化 VSS_Service 除了 VSS.require(["VSS/Service"]?

看起来你的代码是正确的,只是没有使用 RestClient 作为变量,尝试修改

var RestClient = VSS_Service.getCollectionClient(TFS_Wit_WebApi.WorkItemTrackingHttpClient);

var witClient = VSS_Service.getCollectionClient(TFS_Wit_WebApi.WorkItemTrackingHttpClient);

有用的文章:https://www.visualstudio.com/en-us/docs/integrate/extensions/get-started/node

我制作了 witClient 和外部变量,这样我就可以在我的程序中的任何地方使用它,并将控件和网格添加到有效的函数中。

var witClient;
  VSS.init({
        explicitNotifyLoaded: true,
        usePlatformScripts: true, 
        usePlatformStyles: true
    });
  VSS.require([
      "VSS/Controls", "VSS/Controls/Grids",
      "VSS/Service", "TFS/WorkItemTracking/RestClient"],
      function (Controls, Grids, VSS_Service, TFS_Wit_WebApi) {
          //get the REST client
          witClient = VSS_Service;//.getCollectionClient(TFS_Wit_WebApi.WorkItemTrackingHttpClient);
          witClient = witClient.getCollectionClient(TFS_Wit_WebApi.WorkItemTrackingHttpClient);
      }
  );