简体   繁体   English

Angular2 ES6 / ES2015 Babel-Angular 2 @符号的语法错误

[英]Angular2 ES6/ES2015 Babel - Syntax error with Angular 2 @ symbol

Babel CLI is giving me this error whilst running it in the terminal: Babel CLI在终端中运行该错误时给我这个错误:

> Benjamins-MacBook-Pro:public Ben$ npm run build
    > 
    > > angular2-quickstart@1.0.0 build /Users/Ben/Development/whatwegrowangular2/public
    > > babel boot.js -d lib
    > 
    > SyntaxError: boot.js: Unexpected token (5:0)   3 |    4 | //
    > Annotation section
    > > 5 | @Component({
    >     | ^   6 |   selector: 'my-app',   7 |   template: '<h1>Hello {{ name }}</h1>'   8 | })

I am using ES6/ES2015, and Angular 2. 我正在使用ES6 / ES2015和Angular 2。

Visual studio code is my development environment. Visual Studio代码是我的开发环境。

Any Ideas on the issue? 关于这个问题有什么想法吗? The error is on the "@" symbol just before component. 错误出现在组件前面的“ @”符号上。

Package.json file: Package.json文件:

{
  "name": "angular2-quickstart",
  "version": "1.0.0",
  "scripts": {
    "start": "npm run lite",
    "lite": "lite-server",
    "build": "babel boot.js -d lib"
  },
  "license": "ISC",
  "dependencies": {
    "angular2": "2.0.0-beta.0",
    "systemjs": "0.19.6",
    "es6-promise": "^3.0.2",
    "es6-shim": "^0.33.3",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.0",
    "zone.js": "0.5.10"
  },
  "devDependencies": {
    "babel-cli": "^6.3.17",
    "lite-server": "^1.3.1"
  }
}

You need to enable ES7 decorator support 您需要启用ES7装饰器支持

Support for @Class decorators (ie the cause of your error) can be enabled the option flags. 可以启用选项标志来支持@Class装饰器(即,导致错误的原因)。

$ babel --optional es7.decorators

Decorators are used extensively in Typescript to allow the language to mimic the characteristics of traditional statically typed OOP languages. 装饰器在Typescript中得到广泛使用,以允许该语言模仿传统的静态类型OOP语言的特征。 @Class decorators act as a higher order factory function, @Property decorators allow the use of getters/setters in JS, etc. @Class装饰充当高阶工厂函数, @Property装饰允许使用的吸气剂/在JS制定者等

Angular2 uses them extensively because it's written in Typescript. Angular2广泛使用它们,因为它是用Typescript编写的。 Despite that, decorators are only at the ' proposal ' stage of possibly being included in the future ES7 specification . 尽管如此,装饰器仅处于“ 提议 ”阶段,可能会包含在将来的ES7规范中

Source: 资源:

Related: 有关:

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

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