【发布时间】:2017-05-28 00:25:05
【问题描述】:
utility.ts
let books = [
{ id: 1, title: "the dark window", author: "krishna", available: true },
{ id: 2, title: "naked eye", author: "sydney", available: false },
{ id: 3, title: "half girlfriend", author: "chetan", available: true },
{ id: 4, title: "make my dreams", author: "salam", available: false }
];
export { books };
main-app.ts
import {books} from './utility';
let getAllBooks = (): void => {
books.filter((val) => {
console.log(val.author);
})
}
如何在 Html 页面中访问 getAllBooks 功能? 如果我不使用导出和导入,它工作得很好,但我必须使用它而不是将所有内容都写入一个文件。
请建议 [使用 amd 模块] 生成一个 JS 文件作为输出 [main.js]。 在 chrome 控制台中出现以下错误。
[(index):13 Uncaught ReferenceError: getAllBooks is not defined 在 HTMLInputElement.onclick ((index):13)]
我的html页面
<html>
<title>Welcome</title>
<head>
<script data-main="./js/main" src="./js/require.js"></script>
<script src="./js/main.js"></script>
<body>
<div id="mytest"></div>
<input type="button" value="Click!" id="btnClick" onclick="getAllBooks();" />
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</body>
</head>
</html>
main.js
define("utility", ["require", "exports"], function (require, exports) {
"use strict";
let books = [
{ id: 1, title: "the dark window", author: "krishna", available: true },
{ id: 2, title: "naked eye", author: "sydney", available: false },
{ id: 3, title: "half girlfriend", author: "chetan", available: true },
{ id: 4, title: "make my dreams", author: "salam", available: false }
];
exports.books = books;
});
define("app", ["require", "exports", "utility"], function (require, exports, utility_1) {
"use strict";
let getAllBooks = () => {
utility_1.books.filter((val) => {
console.log(val.author);
});
};
});
//# sourceMappingURL=main.js.map
【问题讨论】:
-
能否给我们看一下内容输出的 main.js 文件?
-
我认为直接在 HTML 中混合模块加载器和事件定义是一个相当糟糕的主意...
-
@Hjort-e 我已经包含了 main.js 文件
标签: node.js typescript