如何$注入角类W / ES6 [英] How to $inject into Angular class w/ES6

查看:96
本文介绍了如何$注入角类W / ES6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

采用了棱角分明1.4 ES6 / 7和巴贝尔,我可以成功注入参数与类块在此之后code名为Controller类:结果

 类控制器{
    构造函数($范围,$状态,$窗口){...}
    ...
}
控制器。$注射=$范围,$状态,$窗口]

然而,这将是清洁器看到合适的构造上方喷出的参数。我见过别人用静态$注射,但我得到一个错误。下面是我试图:

 类控制器{
    静态$注射=$范围,$状态,$窗口]
    构造函数($范围,$状态,$窗口){...}
    ...
}

为什么会造成这个错误?这似乎是替别人打工。结果

 意外的令牌(2:11)
  1 |类控制器{
  2 |静态$注射=$范围,$状态,$窗口]
    |
              ^


解决方案

这是一个实验提出的语法。在通天,你必须启用 es7.classProperties 。通

 可选:['es7.classProperties']

通天塔。确切的方法取决于你如何transpiling。

如果你想要做标准ES6,你也可以做

 静态get $注射(){返回[$范围,$状态,$窗口]; }

Using Angular 1.4 with ES6/7 and Babel, I can successfully inject parameters into a class named Controller with this code after the class block:

class Controller {
    constructor($scope, $state, $window) {...}
    ...
}
Controller.$inject = ["$scope", "$state", "$window"]

However, it would be cleaner to see the inject parameters right above the constructor. I've seen other people use static $inject, but I get an error. Here's what I'm attempting:

class Controller {
    static $inject = ["$scope", "$state", "$window"]
    constructor($scope, $state, $window) {...}
    ...
}

Why does that cause this error? It seems to work for other people.

Unexpected token (2:11)
  1 | class Controller {
  2 |     static $inject = ["$scope", "$state", "$window"]
    |  
              ^

解决方案

That is an experimental proposed syntax. In Babel, you'd have to enable es7.classProperties. Pass

optional: ['es7.classProperties']

to babel. The exact method depends on how you are transpiling.

If you wanted to do standard ES6, you could also do

static get $inject(){ return ["$scope", "$state", "$window"]; }

这篇关于如何$注入角类W / ES6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆