【问题标题】:missing android package in nativescript using typescript使用打字稿在nativescript中缺少android包
【发布时间】:2016-09-05 20:55:21
【问题描述】:

我收到以下错误消息:

main-page.ts(15,26): error TS2304: Cannot find name 'android'.

使用 typescript 创建新的 nativescript 项目后。

tns create demo --template typescript

并添加以下xml、ts文件。

主页.xml

<Page xmlns="http://schemas.nativescript.org/tns.xsd">
  <StackLayout>
    <Placeholder creatingView="creatingView"/>
  </StackLayout>
</Page>

main-page.ts

import placeholder = require("ui/placeholder");

export function creatingView(args: placeholder.CreateViewEventData) {
    var nativeView = new android.widget.CalendarView(args.context);
    args.view = nativeView;
}

【问题讨论】:

    标签: typescript nativescript


    【解决方案1】:

    这是一个 TypeScript 编译器警告,它本身不是错误。如果您想阻止这些警告,您可以将平台定义安装到您的 Nativescript 项目中:npm i tns-platform-declarations

    您仍然可以运行 TSC 将 TypeScript 转换为 Javascript 并且它可以工作。它只是用于类型检查警告,这是 TypeScript 的好处:)

    希望所有这些都有意义并有所帮助。

    https://www.npmjs.com/package/tns-platform-declarations

    【讨论】:

    • 谢谢@BradMartin,在安装 tns-platform-declarations 包并将以下 sn-p 添加到 tsconfig.json "files" 后: [ "/node_modules/tns-platform-declarations/android17.d .ts" ] 编译器找到了 android 包。
    • 您介意接受答案,以便可以在 SO 线程上解决此问题吗?
    • 我为此苦苦挣扎了很久。对于其他任何人 - 如果您正在使用 typescript 并且您的编译失败,因为它找不到某些类/函数(如 android.support.v4.content.ContextCompat.checkSelfPermission),请将其包装为 any - (android.support.v4.content.ContextCompat as any).checkSelfPermission
    • 更简单的是declare var android: any,所以每次使用android命名空间时都不需要强制转换。
    猜你喜欢
    • 2021-09-23
    • 2021-04-29
    • 2013-09-17
    • 2018-09-30
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 2017-11-18
    • 1970-01-01
    相关资源
    最近更新 更多