Azure devops 如何在Azure devops扩展开发中读取外部json文件?

Azure devops 如何在Azure devops扩展开发中读取外部json文件?,azure-devops,requirejs,azure-devops-extensions,Azure Devops,Requirejs,Azure Devops Extensions,我试图读取项目“index.html”文件中的json文件,因为对于azure devops扩展,我们已经有require.js库,因此希望使用它的相同功能导入“index.html”文件中的“config.json”文件 基本文件结构: |-index.html |-静态|-icon.png |   |-config.json |-vss-extension.json 我的index.html文件看起来有点像这样: 初始化块 VSS.init({ explicitNotifyL

我试图读取项目“index.html”文件中的json文件,因为对于azure devops扩展,我们已经有require.js库,因此希望使用它的相同功能导入“index.html”文件中的“config.json”文件

基本文件结构:
|-index.html
|-静态|-icon.png
|   |-config.json
|-vss-extension.json

我的index.html文件看起来有点像这样:

初始化块

VSS.init({
        explicitNotifyLoaded: true,
        usePlatformScripts: true,
        setupModuleLoader: true,
        moduleLoaderConfig: {
          paths: {
            "Static": "static"
          }
        }
});
VSS.require(
        ["TFS/WorkItemTracking/Services", "Static/config"],
        function (_WorkItemServices, ConfigJson) {

        VSS.ready(function(){
            VSS.register(VSS.getContribution().id, function () {
              return {
                // Called when the active work item is modified
                onFieldChanged: function (args) {
                  console.log(
                    "inside onfield : " +
                      JSON.stringify(ConfigJson)
                  );
                }
                ....
              };
            });

            VSS.notifyLoadSucceeded();
        })
});
"files": [
    {
      "path": "static/config.json",
      "addressable": true,
      "contentType": "application/json"
    },
    ....
  ]
需要块

VSS.init({
        explicitNotifyLoaded: true,
        usePlatformScripts: true,
        setupModuleLoader: true,
        moduleLoaderConfig: {
          paths: {
            "Static": "static"
          }
        }
});
VSS.require(
        ["TFS/WorkItemTracking/Services", "Static/config"],
        function (_WorkItemServices, ConfigJson) {

        VSS.ready(function(){
            VSS.register(VSS.getContribution().id, function () {
              return {
                // Called when the active work item is modified
                onFieldChanged: function (args) {
                  console.log(
                    "inside onfield : " +
                      JSON.stringify(ConfigJson)
                  );
                }
                ....
              };
            });

            VSS.notifyLoadSucceeded();
        })
});
"files": [
    {
      "path": "static/config.json",
      "addressable": true,
      "contentType": "application/json"
    },
    ....
  ]
我的vss扩展名.json文件:

文件块

VSS.init({
        explicitNotifyLoaded: true,
        usePlatformScripts: true,
        setupModuleLoader: true,
        moduleLoaderConfig: {
          paths: {
            "Static": "static"
          }
        }
});
VSS.require(
        ["TFS/WorkItemTracking/Services", "Static/config"],
        function (_WorkItemServices, ConfigJson) {

        VSS.ready(function(){
            VSS.register(VSS.getContribution().id, function () {
              return {
                // Called when the active work item is modified
                onFieldChanged: function (args) {
                  console.log(
                    "inside onfield : " +
                      JSON.stringify(ConfigJson)
                  );
                }
                ....
              };
            });

            VSS.notifyLoadSucceeded();
        })
});
"files": [
    {
      "path": "static/config.json",
      "addressable": true,
      "contentType": "application/json"
    },
    ....
  ]
我总是收到require.js脚本错误:

参考:

  • 用于vss扩展名文件
  • 对于index.html

  • 恐怕您无法直接获取json文件的内容

    但是您可以尝试使用HTTP请求来获取内容

    请参考以下样本:

     onFieldChanged: function (args) {
                            var request = new XMLHttpRequest();
                            request.open('GET', 'config.json', true);
                            request.send(null);
                            request.onreadystatechange = function () {
                                if (request.readyState === 4 && request.status === 200) {
                                    var type = request.getResponseHeader('Content-Type');
                                                    console.log( "inside onfield : " + JSON.stringify(request.responseText));
                                }
                            }
    
    看看这两张票,了解详情


    VSS是否使用未修改的RequireJS?如果是,则可以使用JSON插件,这将有助于:

    使用它非常简单,只需添加前缀
    json将json文件指定为依赖项时:

    VSS.require(
    [“TFS/WorkItemTracking/Services”,“json!Static/config”],
    函数(_WorkItemServices,ConfigJson){
    VSS.ready(函数(){
    VSS.register(VSS.getContribution().id,函数(){
    返回{
    //修改活动工作项时调用
    onFieldChanged:函数(args){
    console.log(
    “内部onfield:”+
    stringify(ConfigJson)
    );
    }
    ....
    };
    });
    VSS.notifyLoadSuccessed();
    })
    });
    
    Hi@Sankalp Sinha。这张票有更新吗?如果答案能给你一些帮助,请随时告诉我。只是想提醒一下.Hi@Kevin Lu MSFT谢谢你的回复,我之前已经提到过那篇文章,但我想重用已经可用的require js的功能,我通过将config.json转换为config.js解决了这个问题,并使用AMD在define中输入对象,就像这样define({})@桑卡普辛哈很棒。如果有任何问题,你可以在这里找到我。很乐意帮忙