【发布时间】:2018-12-06 03:48:48
【问题描述】:
我的网站项目中的导入语句有问题。尝试使用 nodejs - typescript 和 jquery
我的项目文件夹如下所示:
- 项目
- node_modules
- 公开
- js
- jquery/dist(复制形式 node_modules/jquery/dist)
- index.html
- ts
- test.ts
- package.json
- tsconfig.json
package.json:
{
"name": "testclient",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@types/jquery": "^3.3.4",
"jquery": "^3.3.1"
},
"devDependencies": {
"typescript": "^2.9.2"
}
}
tsconfig.json:
{
"compileOnSave": true,
"compilerOptions": {
"experimentalDecorators": true,
"sourceMap": true,
"strict": true,
"module": "ES2015",
"moduleResolution": "Classic",
"target": "ES5",
"lib": ["dom", "es5", "es2015", "es2015.promise"],
"removeComments": true,
"sourceMap": true,
"outDir": "public/js",
"allowSyntheticDefaultImports": true
},
"include": [
"ts/**/*"
],
"exclude": [
"node_modules"
]
}
index.html
<!DOCTYPE html>
<html lang="de">
<header>
<title>Test</title>
</header>
<body>
Mein Test
</body>
<script type="text/javascript" src="js/jquery/dist/jquery.min.js"></script>
<script type="text/javascript" src="js/test.js"></script>
</html>
test.ts
import $ from 'jquery';
$(document).ready(() => {
console.log('document is ready now');
console.log($);
});
如果我在我的 chrome 浏览器中启动 index.html 并打开控制台,我会收到错误:
Uncaught SyntaxError: Unexpected identifier test.js:1
我发现 $ 在我称之为 jquery 的那一刻是未知的。所以我不能在我的脚本中使用 jquery
在不使用 ///<reference path=""/> 导入 jquery 的情况下运行它的最佳做法是什么。要设置哪些 tsconfig 参数以使其在浏览器中运行?
【问题讨论】:
-
顺便说一句,如果我将 test.ts:
import $ from 'jquery更改为/// <reference path="../node_modules/@types/jquery/index.d.ts"一切正常。但这不是我搜索的方式。我认为导入语句必须起作用
标签: javascript jquery node.js typescript