How to expose angular 2 methods publicly?

前端 未结 5 1372
再見小時候
再見小時候 2020-11-28 09:40

I am currently working on porting a Backbone project to an Angular 2 project (obviously with a lot of changes), and one of the project requirements requires certain methods

5条回答
  •  失恋的感觉
    2020-11-28 10:11

    The problem is that Angular's components are transpiled into modules that aren't as easy to access as regular JavaScript code. The process of accessing a module's features depends on the module's format.

    An Angular2 class can contain static members that can be defined without instantiating a new object. You might want to change your code to something like:

    @component({...})
    class MyTest {
        private static text: string = '';
        public static setText(text:string) {
            this.text = text;
        }
    }
    

提交回复
热议问题