在 Typescript Ionic 项目中包含 Javascript 文件

问题描述 投票:0回答:1

这花了我几个小时来解决,我阅读了很多过时的帖子,所以我发布了一个新问题。 如何使用

d.ts
文件在 Ionic 项目中添加和导入 javascript 文件?

javascript typescript ionic-framework
1个回答
0
投票

经过多次试验,我从 Ionic Appflow Dashboard 创建了一个新的 Ionic React 应用程序,并尝试使用一些现有的 JS 文件。这是一个基本示例。

//test.js
export function testMsg(msg){
    alert(msg)
}
//test.d.ts
declare module 'test' {
    export function testMsg(msg) : void;
  }
//Import and use
import {testMsg} from './test'

testMsg('test');

可视化代码抱怨 js 文件中的参数具有“any”类型,并且导入的文件不是模块,但代码可以工作。

我在

compilerOptions
中的
tsconfig.json

下添加了两行
  "compilerOptions": {
    ...
    "allowJs": true,
    "checkJs": true,
© www.soinside.com 2019 - 2024. All rights reserved.