[英]typescript: resolving typeahead.js with typescript and webpack 2
I am getting the following error from webpack. 我从webpack收到以下错误。
ERROR in ./wwwroot/js/admin/infrastructure/typeaheadComponent.ts Module not found: Error: Can't resolve 'typeahead' in ...
找不到./wwwroot/js/admin/infrastructure/typeaheadComponent.ts中的错误:错误:无法解析...中的“ typeahead”
I have the following installed 我已安装以下内容
npm install typeahead.js
npm install @types/typeahead
My typescript is as follows, using node
module resolution. 我的打字稿如下,使用
node
模块解析。
import { module } from "angular";
import "typeahead";
// necessary to import typeahead into JQuery, as otherwise
// typeahead below is not defined.
class TypeAheadController {
foo(e) {
$(e).typeahead(...)
}
}
this generates javascript as follows: 这将生成JavaScript,如下所示:
"use strict";
var angular_1 = require("angular");
require("typeahead");
var TypeAheadController = (function () { ...
My webpack.config.js is as follows: 我的webpack.config.js如下:
module.exports = {
context: __dirname,
entry: [
"./app.ts",
"./tab.ts",
"./client/clientService.ts",
"./client/clientSearchComponent.ts",
"./infrastructure/messageComponent.ts",
"./infrastructure/typeaheadComponent.ts",
"./url.ts"],
output: {
filename: "./wwwroot/js/admin/admin.js"
},
devtool: "source-map",
module: {
rules: [
{ test: /\.ts$/, use: 'ts-loader' }
]
}
};
imported into a gulp task. 导入了一个gulp任务。
How do I specify that typeahead
is located in node_modules/typeahead.js/dist/typeahead.bundle.js
我如何指定
typeahead
位于node_modules/typeahead.js/dist/typeahead.bundle.js
The module is called typeadhead.js
so you also need to import typeahead.js
, not typeahead
. 该模块称为
typeadhead.js
因此您还需要导入typeahead.js
,而不是typeahead
。
import "typeahead.js";
The import is always the same as the name you use to install it with npm. 导入始终与您使用npm安装它的名称相同。 And it's not even special, it simple looks into
node_modules
and finds the directory with the given name. 而且甚至没有什么特别之处,它可以简单地查看
node_modules
并找到具有给定名称的目录。 Then it looks into package.json
and imports the file specified in the main
field . 然后,它查看
package.json
并导入在main
字段中指定的文件。 See also Node.js - Folders as Modules . 另请参见Node.js-文件夹作为模块 。
You could use resolve.alias
to change the name of the import, but there is not really a good reason for doing that in this case. 您可以使用
resolve.alias
更改导入的名称,但是在这种情况下,并没有充分的理由。
You could solve this using aliasing. 您可以使用别名解决此问题。 Minimal example of what to change in your
webpack.config.js
: 在
webpack.config.js
更改内容的最小示例:
module.exports = {
/* ... everything you currently have */
resolve: {
alias: {
typeahead: 'typeahead.js'
}
}
}
I resolved this by making the following changes. 我通过进行以下更改来解决此问题。
You need to import Bloodhound and Typeahead seperately. 您需要分别导入Bloodhound和Typeahead。 To do this edit your
webpack.config.js
为此,请编辑您的
webpack.config.js
resolve: {
extensions: ['.js', '.ts'],
alias: {
typeahead: 'corejs-typeahead/dist/typeahead.jquery.min.js',
bloodhound: 'corejs-typeahead/dist/bloodhound.min.js'
}
},
And then in your .ts
file: 然后在您的
.ts
文件中:
import "typeahead";
import * as Bloodhound from "bloodhound";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.