创建NativeScript插件时的index

创建NativeScript插件时的index

本文介绍了创建NativeScript插件时的index.d.ts?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始在NativeScript中制作一个插件-遵循文档.我使用src文件夹中的npm run demo.ios成功运行了一些.

I have recently started to make a plugin in NativeScript - following the documention. And I made some successfull runs with npm run demo.ios in the src folder.

但是,当开始构建我打算使用的实际插件时-我遇到以下错误:

However when starting to build the actual plugin I intended on - I am encountering the following error:

我已经在我尝试从演示应用程序调用的myplugin.common.ts文件中的ViewBase上设置了扩展方法

I have set up an extension method onto ViewBase in my myplugin.common.ts file that I attempt to call from the demo application

我认为问题可能出在index.d.ts上.根据文档,myplugin.ios.d.tsmyplugin.android.d.ts文件应复制到此处或手动定义.我正尝试在其中复制myplugin.common.d.ts文件内容.那有效吗?

The problem could lie in the index.d.ts I believe. According the docs the myplugin.ios.d.ts and myplugin.android.d.ts files should be copied in there or defined manually. I am attempting to copy the myplugin.common.d.ts file content in there instead. Is that valid?

我还注意到,两次运行似乎都是necceseray-因为第一次运行会生成...d.ts.,然后必须将其中的内容复制到index.d.ts.

I have also noticed two runs seem to be necceseray - since the first run generates the ...d.ts. Of which content then has to copied into index.d.ts.

一些代码...

myplugin.common.ts

myplugin.common.ts

declare module "tns-core-modules/ui/core/view-base/view-base" {
  interface ViewBase {
    myExenstionMethod(myNumber: number): void;
  }
}
ViewBase.prototype.myExenstionMethod = function(myNumber: number) {
  console.log("myNumber: " + myNumber);
}

index.d.ts(从myplugin.common.d.ts复制):

declare module "tns-core-modules/ui/core/view-base/view-base" {
    interface ViewBase {
        myExenstionMethod(myNumber: number): void;
    }
}

它在演示项目中显示:

错误来自简单地从演示项目中的插件中导入扩展方法:

And the error comes from simply importing the extension method from the plugin in my demo project:

import "nativescript-myplugin";


My conclusion so far is that I

推荐答案

TypeScript声明(*.d.ts)仅出于对IDE的IntelliSense支持的考虑.因此,只要您导出要公开的权限定义,就没有所谓的有效或无效.大多数插件只会导出index.d.ts,通常会显示*-common.ts中的所有内容.

TypeScript declarations (*.d.ts) are just for the sake of IntelliSense support with your IDE. Hence there is nothing called valid or invalid as long you export right definitions you want to expose. Most plugins will just export index.d.ts that will usually expose everything from *-common.ts.

更新:

为了防止生成.d.ts.js.map文件,您将设置declaration& src/tsconfig.jsonfalse中的sourceMap标志.

In order to prevent generating .d.ts and .js.map files, you will set declaration & sourceMap flags in src/tsconfig.json to false.

您不能避免使用.js文件,这是使用TypeScript编译器(将TS转换为JS)的唯一原因.发布插件或在演示应用程序中使用它时,.js文件是实际使用的文件.

You can not avoid .js files, that's the only sole reason of using TypeScript compiler (Converting TS to JS). While publishing your plugin or using it in demo app, .js files are the ones actually used.

之间,没有sourceMap调试可能对您来说很困难.

Between, without sourceMap debugging may be difficult for you.

这篇关于创建NativeScript插件时的index.d.ts?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 05:33