简体   繁体   English

BuildRestClient 导入导致问题 - Azure DevOps

[英]BuildRestClient import causes problems - Azure DevOps

Currently i'm trying to get the workitems of my azure devops server with following code:目前我正在尝试使用以下代码获取我的 azure devops 服务器的工作项:

export function geWorkitems(target: HTMLElement): void {
    // Get an instance of the client
    let client = WorkitemRestClient.getClient();
    client.getWorkItems([1]).then(definitions => {
        target.innerText = JSON.stringify(definitions)
    }
    );
}
show("workitems", geWorkitems);

when I include the needed import its gives me this error:当我包含所需的导入时,它给了我这个错误:

Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead.ts(1202)

The import i am talking about:我正在谈论的导入:

import WorkitemRestClient = require("TFS/WorkItemTracking/RestClient");

People have been suggesting to write it this way, but it still doesnt work人们一直建议这样写,但它仍然不起作用

import WorkitemRestClient from "TFS/WorkItemTracking/RestClient";

same with import BuildRestClient = require("TFS/Build/RestClient");import BuildRestClient = require("TFS/Build/RestClient");

Best method最佳方法

Have you tried the code suggested in the error message ( import {a} from "mod" ):您是否尝试过错误消息中建议的代码( import {a} from "mod" ):

import { geWorkitems } from "TFS/WorkItemTracking/RestClient";

Make sure you have the correct spelling and capitalisation because the export name must be the same as the import确保您的拼写和大小写正确,因为导出名称必须与导入名称相同


Alternative 1备选方案 1

You could also try:你也可以试试:

const WorkitemRestClient = require("TFS/WorkItemTracking/RestClient");

using require is like normal assignments so you can't use the import keyword.使用 require 就像正常的分配,所以你不能使用 import 关键字。

require is less used nowadays and works better with the module.exports = {} notation require 现在很少使用,并且与module.exports = {}表示法一起使用效果更好


Alternative 2备选方案 2

Finally, you could keep the second import you mentionne:最后,您可以保留您提到的第二个导入:

import WorkitemRestClient from "TFS/WorkItemTracking/RestClient";

but you need to export it differently with the default keyword:但是您需要使用default关键字以不同方式导出它:

export default function geWorkitems(target: HTMLElement): void {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM