Ionic-基于IsDebugMode更改导入库 [英] Ionic - Change import library based on IsDebugMode

查看:92
本文介绍了Ionic-基于IsDebugMode更改导入库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用 IsDebugMode 切换出导入库吗?

Can I use IsDebugMode to switch out import libraries?

我可以这样做:

if (IsDebugMode) {
    import { SQLiteMock, SQLiteObject } from '../mocks/SQLiteMock';
}
else {
    import { SQLite, SQLiteDatabaseConfig, SQLiteObject } from '@ionic-native/sqlite';
}

推荐答案

否,很遗憾,您不能有条件地导入库.但是,您可以做的是导入两个库,同时使用

No, unfortunately you can't conditionally import libraries. What you can do though is import both libraries, inject both in your constructor and then conditionally using isDevMode(), (there exists no isDebugMode()) you can use whatever you prefer for each case.

但这不是一个很好的解决方案,因为这意味着您将两个库都加载到内存中,并且由于将它们注入到构造函数中,因此在构建时发生的树状摇动不会忽略它们.

This is not a very good solution though, as it means you will load both libs in memory and since you inject them in the constructor the treeshaking that happens at build time will not ommit them.

如果少量执行此操作,则它可能甚至没有作用(负面影响).我建议您在运行时使用开发工具对内存大小进行基准测试,并且如果要获得的收益超过了清理程序的手动性能,那么在使用该Mock开发功能之后,只需在导入中替换Mock类即可.

If this is done sparingly it might even not make a difference though (negatively). I suggest you benchmark Memory size at runtime using dev tools and if there is significant gain to outweigh the manual nature of the cleaner approach, then just replace the Mock class in the import when you are done developing the feature using that Mock.

这篇关于Ionic-基于IsDebugMode更改导入库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆