javascript - 为什么 requireJS 不在模块名称中附加 '.js'

标签 javascript requirejs

我的以下代码:

require(['/AppScripts/book/properties'], function (mod) {
    ...
});

失败,因为它发送的 get 请求在 url 中没有 .js,如下所示:
http://localhost:182/AppScripts/book/properties

同样,由于同样的原因,定义也失败了:

define(["require", "exports", "global"], function (require, exports, global_1) {
    ...
});  

生成 http://localhost:182/global错过了 .js

我通过添加“.js”仔细检查了两个网址,他们找到了所需的脚本。

这是我的配置:

requirejs.config({
    paths: {
        jquery: '../Scripts/lib/jquery',
        requirejs: '../Scripts/lib/require',
    }
});

我能找到的所有文档和引用资料都说扩展名是自动附加的,但在我的情况下不是。我的问题是,这段代码是由 TypeScript 编译器生成的,所以我无法手动添加扩展。它必须由 requireJS 处理。

有人可以帮助我缺少什么吗?谢谢。

编辑
这是我的 tsconfig:

{
  "compileOnSave": true,
  "compilerOptions": {
    "module": "AMD",
    "noImplicitAny": true,
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "ES5",
    "outDir": "../AppScripts",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "allowSyntheticDefaultImports": true,
    "skipLibCheck": true,
    //"watch": true,
    "baseUrl": "./",
    "paths": {
      "*": [ "TypeScripts/typings/*" ]
    },
    "typeRoots": [
      "TypeScripts/typings"
    ]
  },
  "exclude": [
    "node_modules",
    "wwwroot",
    "TypeScripts/typings"
  ]
}

最佳答案

requirejs 将从“/”开始的路径视为绝对路径,因此不会在字符串的左侧或右侧附加任何内容,而我正在寻找不能从“/”开始的相对路径。将 require(['/AppScripts/book/properties'] 更改为 require(['AppScripts/book/properties'] 解决了问题。

关于javascript - 为什么 requireJS 不在模块名称中附加 '.js',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61368733/

相关文章:

JavaScript 抛出 TypeError 说我的变量未定义

javascript - 让我的 javascript 函数 requirejs/amd 友好?

javascript - 在使用 requireJs 调用的方法中使用 Node require

javascript - 在 RequireJS 中为模块 ID 分配 "shortcut"?

javascript - MeshLab Quadric Edge Collapse Decimation 在 JavaScript、PHP 或 Python 中保留纹理?

javascript - 数据表格式无效: must have at least 2 columns

jquery - 带有 jQ​​uery 验证的 RequireJS - 设置默认设置

javascript - Dojo 声明 - 默认属性的范围(实例或静态)

javascript - 将选定选项设置为多选jquery

javascript - 在 Javascript 中动态调用函数