如何在Angular8中创建具有匿名函数的类

问题描述 投票:0回答:1

我正在开发Angular 8应用程序,需要使用一个库来显示3D模型(JS + WASM)。为了在Tree Table和3D模型之间进行交互,我需要在该库中注册一个观察者。

用于注册观察者的给定代码样本(针对该库)用AngularJS编写:

注册:

$scope.RegisterSelectionObserver = function() {
    if ($scope.selectionObserver == null) {
        $scope.selectionObserver = new $scope.MySelectionClass();
        $scope.session.RegisterSelectionObserver($scope.selectionObserver);
    }
}

类定义:

$scope.MySelectionClass = Module.SelectionEvents.extend("SelectionEvents", {
        __construct: function() {
            this.__parent.__construct.call(this);
            this.SetEventsFilter(Module.EVENTS_PICKS | Module.EVENTS_SELECTION);
        },

        OnSelectionBegin: function () {
            if ($scope.webglSettings.selectionLogging === 'YES') {
                console.log("OnSelectionBegin");
            }
        },
     });

我的收养:我试图用其构造函数创建一个类,并将其传递给观察者注册,但出现错误。

我的自定义类:

export class ExtSelectionEvents{
    constructor(){
    }

    OnSelectionBegin(){
        console.log('OnSelectionBegin');
    }
}

我的注册:

const extSelectionEventsInstance = new ExtSelectionEvents();
session.RegisterSelectionObserver(extSelectionEventsInstance);

错误:

zone.js:703未处理的承诺拒绝:无法传递“ [object Object]”作为SelectionEvents *;区域:任务:Promise.then;值:BindingError。{名称:“ BindingError”,消息:“无法传递” [对象对象]“作为SelectionEvents *”,堆栈:“ BindingError:无法通过顺序出现“ [object Object]”(http://localhost:4200/scripts.js:181:13)“}消息:“无法通过“ [对象对象]”作为SelectionEvents *”名称:“ BindingError”堆栈:“ BindingError:无法将” [object Object]“作为SelectionEvents *↵在BindingError处。(http://localhost:4200/assets/js/libthingview_wasm.js:1:117337)↵在新的BindingError上(在createNamedFunction(http://localhost:4200/assets/js/libthingview_wasm.js:1:116224处为eval,:4:34)↵在throwBindingError(http://localhost:4200/assets/js/libthingview_wasm.js:1:118918)↵在RegisteredPointer.nonConstNoSmartPtrRawPointerToWireType [astoWireType]http://localhost:4200/assets/js/libthingview_wasm.js:1:134368)↵在Session $ RegisterSelectionObserver [作为RegisterSelectionObserver](eval在new_(http://localhost:4200/assets/js/libthingview_wasm.js:1:142970),:8:26)↵OverviewComponent.push ../ SRC /应用/视图/ mechportal /概述/ overview.component.ts.OverviewComponent.callback(http://localhost:4200/views-mechportal-overview-overview-module.js:18109:17)↵在http://localhost:4200/scripts.js:18:31http://localhost:4200/scripts.js:182:17↵位于_loadPreferences(http://localhost:4200/scripts.js:304:9)↵在Object.LoadPreferences(http://localhost:4200/scripts.js:181:13)“ proto:错误BindingError:无法将“ [object Object]”作为SelectionEvents *在BindingError处。 (http://localhost:4200/assets/js/libthingview_wasm.js:1:117337)在新的BindingError处(在createNamedFunction(http://localhost:4200/assets/js/libthingview_wasm.js:1:116224)处,:4:34)

总结:

该库期望为OnSelectionBegin使用匿名函数,因为它正在为其创建命名函数。

[MySelectionClass中发生了什么,如何使用其匿名函数转换AngularJS类定义以与Angular 8 /库一起使用?

angularjs anonymous-function anonymous-class
1个回答
0
投票

不确定,但是您应该处理JS类:

export class ExtSelectionEvents extends SelectionEvents{
    constructor() {
      super();
    }

    OnSelectionBegin(){
        console.log('OnSelectionBegin');
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.