首页 文章

如何在打字稿中导入js文件作为模块

提问于
浏览
0

我正在使用SharePoint中的SPFx框架,它将具有打字稿文件 . 我在我的应用程序中使用SPServices.js和app.js(自定义js文件) . 在config.json文件中我使用如下:

"externals": {
    "jQuery":{
      "path":"node_modules/jquery/dist/jquery.min.js",
      "globalName": "jQuery1"
    },
    "AppScript":{
      "path":"/src/webparts/calendarEvents/app.js",
      "globalName": "AppScript",
      "globalDependencies": [
        "jQuery",
        "SPServices"      
      ]
    },
    "SPServices": {  
      "path": "/src/webparts/calendarEvents/jquery.SPServices.min.js",  
      "globalName": "SPServices" ,
      "globalDependencies": [
        "jQuery"
      ] 
    }
}

如果我使用下面的语法,它在我的.ts文件中工作

require('SPServices')
require('AppScript')

但是,我想将它们作为对象导入而不是上面的语法,以便我可以访问这些文件中的函数 . 为了做到这一点,我正在使用as

import * as SPServices from 'SPServices';
import * as myScript from 'AppScript';

但这些都行不通 . 它给出了错误 .

Resource "SPServices" not found in loader configuration of manifest for component "b052328a-ad75-4056-af7f-7bc8b3e795fc" (CalendarEventsWebPart)

但奇怪的是,同一行 import * as SPServices from 'SPServices'; 在另一个.ts文件中工作正常 . 它没有给出任何这样的错误 . 我真的很困惑这里错了 .

为js,css文件等类型文件添加文件时为什么会有太多混淆?

1 回答

  • 0

    SPServices是在CommonJS模块用于导入功能之前编写的 . 它通过扩展jQuery来工作 . 试试这个:

    import * as jQuery from 'jQuery';
    require('SPServices');
    
    $().SPServices.doStuff();
    

    有关此用例的详细信息,请参阅Microsoft文档部分Loading a library that has a dependency on another library .

相关问题