简体   繁体   English

无法从客户端应用程序引用插件中定义的静态方法

[英]Can't reference static method defined in plugin from an client application

I have js utilities plugin which contains the following class with a static method, but it can't be referenced from an application that uses this utilities plugin. 我有js 实用程序插件 ,其中包含带有静态方法的以下类,但是无法从使用该实用程序插件的应用程序中引用。

 export class Helpers { static generateGUID() { return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, a=>(a ^ Math.random() * 16 >> a / 4).toString(16)); } } 

client application calling Helpers: 客户端应用程序调用助手:

 import {Helpers} from 'utilites'; export class MyClass { constructor() { } test() { //can't reference Helpers console.log(Helpers.generateGUID()); } } 

But if I change the static method to non-static method like below and reference it from a client app, it can be accessible: 但是,如果我将静态方法更改为如下所示的非静态方法并从客户端应用程序引用它,则可以访问它:

 export class Helpers { //removed static generateGUID() { return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, a=>(a ^ Math.random() * 16 >> a / 4).toString(16)); } } 

client application calling Helpers: 客户端应用程序调用助手:

 import {inject} from 'aurelia-framework'; import {Helpers} from 'sp-utilities'; @inject(Helpers) export class MyClass { constructor(helpers) { this.helpers = helpers; } test() { //this works console.log(this.helpers.generateGUID()); } } 

I did confirm that if I use the same Helpers code with a static method within an application, I can call it like Helpers.generateGUID() without instantiating Helpers. 我确实确认,如果我在应用程序中使用相同的Helpers代码和静态方法,则可以将其像Helpers.generateGUID()那样调用,而无需实例化Helpers。 Does anyone know why this static method in my plugin is not accessible when it's referenced from a client application? 有谁知道为什么从客户端应用程序引用插件时无法访问我插件中的此静态方法?

It might be something to do with how I export Helpers class in index.html but I am not sure. 这可能与我如何在index.html中导出Helpers类有关,但我不确定。

 export {Helpers} from './helpers'; export function configure(config) { config.globalResources(); } 

This seems to be a Babel issue, not Aurelia's. 这似乎是通天塔的问题,而不是奥雷利亚的问题。 Please check this link for the solution to your issue: import class and call static method with es6 modules with babel transpiler 请检查此链接以解决您的问题: 导入类并使用带有babel transpiler的es6模块调用静态方法

Also, I've checked your plugin code, you don't need the @noView there. 另外,我已经检查了您的插件代码,那里不需要@noView You need that only for aurelia components, and this are utility classes, so you can safely remove them. 您只需要aurelia组件,而这是实用程序类,因此可以安全地删除它们。

Regards! 问候!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM