我已将项目更新为最新的NativeScript version 4.1.2
。我一直在将Native-android代码直接用于NativeScript应用程序中。
4.0以下的旧版本可以正常工作。但是已更新为最新版本4.0或更高版本,不支持nativescript中的本地类。
app.component.ts文件:
import { Component } from "@angular/core";
declare var org: any;
@Component({
selector: "ns-app",
template: `
<StackLayout orientation="vertical">
<Label [text]="message" (tap)="onTap()"></Label>
</StackLayout>
`
})
export class AppComponent {
public onTap() {
console.log(org.nativescript.Toast.postData());
}
}
平台/ android / app / src / main / java / org / nativescript:
Toast.java:
public class Toast {
public static String postData(){
return "ToastValue";
}
}
最后,我在控制台中收到以下错误:
JS: ERROR TypeError: Cannot read property 'postData' of undefined
JS: ERROR CONTEXT {
JS: "view": {
JS: "def": {
JS: "nodeFlags": 1,
JS: "rootNodeFlags": 1,
JS: "nodeMatchedQueries": 0,
JS: "flags": 0,
JS: "nodes": [
JS: {
JS: "nodeIndex": 0,
JS: "parent": null,
JS: "renderParent": null,
JS: "bindingIndex": 0,
JS: "outputIndex": 0,
JS: "checkIndex": 0,
JS: "flags": 1,
JS: "childFlags": 1,
JS: "directChildFlags": 1,
JS: "childMatchedQueries": 0,
JS: "matchedQueries": {},
JS: "matchedQueryIds": 0,
JS: "references": {},
JS: "ngContentIndex": null,
JS: "childCount": 1,
JS: "bindings": [],
JS: "bindingFlags": 0,
JS: "outputs": [],
JS: "element": {
JS: "ns": "",
JS: "name": "StackLayout",
JS: "attrs": [
JS: [
JS: "",
JS: "orientation",
JS: "vertical"
JS: ]
JS: ],
JS: "template": null,
JS: "componentProvider": null,
JS: "componentView": null,
JS: ...
因此,我无法在控制台中打印ToastValue。