Javascript 未捕获的 ReferenceError:定义未定义打字稿

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38817636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:53:33  来源:igfitidea点击:

Uncaught ReferenceError: define is not defined typescript

javascriptknockout.jstypescriptrequirejs

提问by Prashant Kankhara

I am new to typescript, knockout and requirejs. I have created some demo using this files. Now, I want to implement some minor logic using typescript and knockoutjs.

我是打字稿、淘汰赛和 requirejs 的新手。我使用这些文件创建了一些演示。现在,我想使用打字稿和knockoutjs 实现一些小逻辑。

I have created 4-5 typescript files, which are imported internally. When I run the html file. I am getting the error stating. as Titled

我创建了 4-5 个内部导入的打字稿文件。当我运行 html 文件时。我收到错误说明。如题

enter image description hereCan somebody help me on this error. What I am missing in this code.

在此处输入图片说明有人可以帮我解决这个错误。我在这段代码中缺少什么。

have search on google and spend quite a good time but didn't find the proper solutions. It must be related to requireJS to define all modules. But, as new in requireJS not able to catch up with that. I have also search stackoverflow for the similar error, but it doesn't help me out.

在谷歌上搜索并花了很多时间,但没有找到合适的解决方案。必须与requireJS相关才能定义所有模块。但是,作为新的 requireJS 无法赶上。我也在 stackoverflow 中搜索了类似的错误,但对我没有帮助。

Waiting for the solution

等待解决方案

回答by Tim Perry

Here your TypeScript has compiled happily, to code that will work in a requireJS environment(technically, an AMDenvironment). That means it generates output that assumes that define/require etc all already exist.

在这里,您的 TypeScript 已愉快地编译为可在 requireJS 环境(从技术上讲,是AMD环境)中运行的代码。这意味着它生成的输出假定定义/要求等都已经存在。

The overall answer is that you need to include RequireJS before you depend on your compiled code.

总体答案是,在依赖已编译的代码之前,您需要包含 RequireJS。

Notably the error suggests you've made a separate mistake though: you're depending directly on the RequireJS module scripts (i.e. you have a <script src="my-compiled-code.js"></script>tag in your HTML). That's not how require modules work. Instead, once you've made RequireJS available, you should have a single top-level startup script (either inline in your HTML or as a separate file) that configures RequireJS and then require()'s the top-level files of your application to start everything off. You can load this file either by hand, or with RequireJS's "data-main" attribute.

值得注意的是,该错误表明您犯了一个单独的错误:您直接依赖于 RequireJS 模块脚本(即您<script src="my-compiled-code.js"></script>的 HTML 中有一个标签)。这不是 require 模块的工作方式。相反,一旦你使 RequireJS 可用,你应该有一个单一的顶级启动脚本(在你的 HTML 中内联或作为一个单独的文件)来配置 RequireJS 然后require()是你的应用程序的顶级文件来启动一切离开。您可以手动加载此文件,也可以使用 RequireJS 的“data-main”属性加载。

For example, a minimal HTML looks something like:

例如,一个最小的 HTML 看起来像:

<!DOCTYPE html>
<html>
    <head>
        <script data-main="scripts/main" src="scripts/require.js"></script>
    </head>
    <body>
    </body>
</html>

This loads RequireJS from 'scripts/require.js' and then tells it to load the script at 'scripts/main.js' to start off the loading process (you'll probably want to update both paths - note that data-main doesn't need a .js extension).

这将从“scripts/require.js”加载 RequireJS,然后告诉它在“scripts/main.js”加载脚本以开始加载过程(您可能想要更新两个路径 - 请注意 data-main 不会不需要 .js 扩展名)。

The main script should then be something very simple like:

主脚本应该非常简单,例如:

// Set up any config you need (you might not need this)
requirejs.config({
  basePath: "/scripts"
});

// Tell RequireJS to load your main module (and its dependencies)
require("mainmodule");

Generally, it's not TypeScript problems you're fighting here, it's RequireJS. I'd try spending a bit more time playing with just Require (maybe in pure JavaScript, so it's clearer) and looking at working examples for that, so you can get that bit working first, then add in the rest.

通常,您在这里解决的不是 TypeScript 问题,而是 RequireJS。我会尝试花更多的时间只使用 Require(可能是纯 JavaScript,所以它更清晰)并查看工作示例,因此您可以先让该部分工作,然后再添加其余部分。