javascript–React-Native应用程序的平台特定iosandroid typescript文件的相对导入

javascript–React-Native应用程序的平台特定iosandroid typescript文件的相对导入,第1张

概述我有一个基于React-Native平台的2个不同设计的组件:MyComponent.ios.tsx和MyComponent.android.tsx.虽然当我将我的组件导入MyView.tsx时,它会抱怨.MyView.tsx(5,38):errorTS2307:Cannotfindmodule‘./MyComponent’.我试图将我的tsconfig文件路径修改为以下内容:"path

我有一个基于React-Native平台的2个不同设计的组件:MyComponent.ios.tsx和MyComponent.android.tsx.

虽然当我将我的组件导入MyVIEw.tsx时,它会抱怨.

MyVIEw.tsx(5,38): error TS2307: Cannot find module ‘./MyComponent’.

我试图将我的tsconfig文件路径修改为以下内容:

"paths": {  "*": ["*", "*.ios", "*.androID"]},

虽然我还有同样的问题.

你们中的任何人都知道如何解决这个问题吗?

谢谢

解决方法:

基本上这种方法不能用于一个原因,一旦你的ts文件被编译,相对路径就不再存在,编译器就不再需要添加“.ios”或“.androID”并停止抱怨,所以输出将不起作用阅读纯Js时反应原生方面.

我通过在同一个文件夹中创建一个tyPings(MyComponent.d.ts)文件来实现它,例如:

// This file exists for two purposes:// 1. Ensure that both ios and androID files present IDentical types to importers.// 2. Allow consumers to import the module as if typescript understood react-native suffixes.import DefaultIos from './MyComponent.ios';import * as ios from './MyComponent.ios';import DefaultAndroID from './MyComponent.androID';import * as androID from './MyComponent.androID';declare var _test: typeof ios;declare var _test: typeof androID;declare var _testDefault: typeof DefaultIos;declare var _testDefault: typeof DefaultAndroID;export * from './MyComponent.ios';
总结

以上是内存溢出为你收集整理的javascript – React-Native应用程序的平台特定ios / android typescript文件的相对导入全部内容,希望文章能够帮你解决javascript – React-Native应用程序的平台特定ios / android typescript文件的相对导入所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1118464.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存