【问题标题】:Angular2/nativescript: Issues importing/using Javascript module in a Typescript appAngular2/nativescript:在 Typescript 应用程序中导入/使用 Javascript 模块的问题
【发布时间】:2017-05-23 17:02:58
【问题描述】:

我正在尝试使用位于

的 localstorage NativeScript 模块

https://www.npmjs.com/package/nativescript-localstorage

我有一个从 Web 应用程序复制的 LocalStorage 服务,我想与 NativeScript 应用程序一起使用,我使用 tns 插件添加 nativescript-localstorage 成功安装了模块。我感到困惑的第一件事是:我的应用程序是用 Typescript 编写的,而这个模块是用 Javascript 编写的,我可以在我的应用程序中导入/使用这个模块吗?我在 LocalStorageService 的导入部分添加了以下内容: var localStorage = require('nativescript-localstorage');该应用程序构建良好,但部署后出现以下错误:

错误: 错误:com.tns.NativeScriptException:找不到模块:“nativescript-localstorage”,相对于:app/tns_modules/

然后我尝试: import * as localStorage from 'nativescript-localstorage';然后我收到一条错误消息,说找不到模块 nativescript-localstorage。知道我做错了什么吗?

【问题讨论】:

    标签: angular angular2-nativescript


    【解决方案1】:

    该错误与您导入它的方式无关,而是您的platforms/[platform]/[appname]/app/tns_modules 文件夹不包含该插件。只需tns platform remove [platform]tns platform add [platform]tns run [platform],您应该会看到它。并且可以require它。

    【讨论】:

    • 那么我提到的那个文件夹是否包含插件?
    • 是否需要在app.module.ts中导入localstorage模块?目前我只是在 localStorage 服务中导入它
    • 如果模块甚至不在你的平台文件夹中,那么导入它也会失败,所以让我们首先关注它。添加插件并进行适当的构建后,它应该在 Android 上结束:/platforms/android/src/main/assets/app/tns_modules,在 iOS 上结束:/platforms/ios/ /app/tns_modules
    • 在构建后根本不在我的平台/android 文件夹中。
    猜你喜欢
    • 1970-01-01
    • 2020-06-01
    • 2020-06-25
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2021-01-14
    • 2019-06-11
    • 2020-07-16
    相关资源
    最近更新 更多