我目前正在评估TypeScript,尝试导入jQuery时遇到一些问题。
updateLocation( location ){
this.getGeocode(location.place_id, output => {
this.suggestedLocations = []
this.setPlace(output[0])
})
},
setPlace( selectedLocation ) {
if(!selectedLocation.formatted_address) return;
const data = {
location: {
formattedAddress: selectedLocation.formatted_address,
position: {
lat: selectedLocation.geometry.location.lat(),
lng: selectedLocation.geometry.location.lng()
}
}
};
this.$f7.popup.close('.add-location')
if(this.$refs.autocomplete) this.$refs.autocomplete.$el.disabled = true
this.localLocation = data.location
db.collection("projects")
.doc(this.project.id)
.set(data, {
merge: true
})
.then()
},
报告以下错误:
import * as $ from 'jquery';
const $el = $('#app');
我已经如下安装了jQuery模块:
src/log.ts:2:13 - error TS2349: Cannot invoke an expression whose type lacks a call signature. Type '{ default: JQueryStatic; ajaxSettings: AjaxSettings<any>; A
nimation: AnimationStatic; Callbacks: CallbacksStatic; cssHooks: CSSHooks; cssNumber: PlainObject<boolean>; Deferred: DeferredStatic; ... 62 more ...; when<TR1,
UR1, VR1, TJ1 = any, UJ1 = any, VJ1 = any>(deferredT: TR1 | ... 1 more ... | Thenable<...>, defer...' has no compatible call signatures.
2 const $el = $('#app');
~~~~~~~~~
src/log.ts:1:1
1 import * as $ from 'jquery';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Type originates at this import. A namespace-style import cannot be called or constructed, and will cause a failure at runtime. Consider using a default impo
rt or import require here instead.
并使用以下生成的npm i jquery --save
npm i @types/jquery --save-dev
文件:
tsconfig.json
我正在使用以下版本:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"strict": true,
"esModuleInterop": true
}
}
我想念什么?
答案 0 :(得分:5)
@types/jquery
使用export =
时,导入时需要使用import =
。
更改
import * as $ from 'jquery';
到
import $ = require('jquery');
答案 1 :(得分:0)
对于“模块”:“esnext”,下面可能会有所帮助,
import jQuery from "jquery";
window.$ = window.jQuery = jQuery;