【问题标题】:Angular 2 giving an error Uncaught SyntaxError: Unexpected token <Angular 2 给出错误 Uncaught SyntaxError: Unexpected token <
【发布时间】:2016-10-01 23:46:45
【问题描述】:

我正在尝试在现有项目上运行 Angular 2,并且我有以下代码:

<script src="https://code.angularjs.org/2.0.0-beta.0/angular2-polyfills.js"></script>
<script src="https://code.angularjs.org/tools/system.js"></script>
<script src="https://code.angularjs.org/tools/typescript.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.0/Rx.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.0/angular2.dev.js"></script>
<script type="text/javascript">
    System.config({
        transpiler: 'typescript', 
        typescriptOptions: { emitDecoratorMetadata: true }, 
        packages: {app: {defaultExtension: 'ts'}} 
      });
      System.import('assets/js/angular2/app/script.js')
            .then(null, console.error.bind(console));
</script>

脚本.ts

import {bootstrap}    from 'angular2/platform/browser'
import {FeedComponent} from 'assets/js/angular2/app/feed.component.js'


bootstrap(FeedComponent)

feed.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'feeds',
  template: 'assets/partials/wall/Feeds.html'
})

export class FeedComponent {

}

html

<feeds>
            loading...
          </feeds>

但它给出了一个错误:

core:1 Uncaught SyntaxError: Unexpected token <U @ system.src.js:4597o.execute @ system.src.js:4597i @ system.src.js:4597s @ system.src.js:4597(anonymous function) @ system.src.js:4597(anonymous function) @ feed.component.js:8(anonymous function) @ feed.component.js:22U @ system.src.js:4597c.execute @ system.src.js:4597i @ system.src.js:4597s @ system.src.js:4597(anonymous function) @ system.src.js:4597(anonymous function) @ script.js:3(anonymous function) @ script.js:6U @ system.src.js:4597c.execute @ system.src.js:4597i @ system.src.js:4597n @ system.src.js:4597execute @ system.src.js:4597y @ system.src.js:4597w @ system.src.js:4597p @ system.src.js:4597h @ system.src.js:4597(anonymous function) @ system.src.js:4597run @ angular2-polyfills.js:138zoneBoundFn @ angular2-polyfills.js:111lib$es6$promise$$internal$$tryCatch @ angular2-polyfills.js:1511lib$es6$promise$$internal$$invokeCallback @ angular2-polyfills.js:1523lib$es6$promise$$internal$$publish @ angular2-polyfills.js:1494(anonymous function) @ angular2-polyfills.js:243run @ angular2-polyfills.js:138zoneBoundFn @ angular2-polyfills.js:111lib$es6$promise$asap$$flush @ angular2-polyfills.js:1305
system.src.js:4597 Uncaught Uncaught SyntaxError: Unexpected token <
    Evaluating http://localhost/v010-commonsocialnetwork/@angular/coreU @ system.src.js:4597o.execute @ system.src.js:4597i @ system.src.js:4597s @ system.src.js:4597(anonymous function) @ system.src.js:4597(anonymous function) @ feed.component.js:8(anonymous function) @ feed.component.js:22U @ system.src.js:4597c.execute @ system.src.js:4597i @ system.src.js:4597s @ system.src.js:4597(anonymous function) @ system.src.js:4597(anonymous function) @ script.js:3(anonymous function) @ script.js:6U @ system.src.js:4597c.execute @ system.src.js:4597i @ system.src.js:4597n @ system.src.js:4597execute @ system.src.js:4597y @ system.src.js:4597w @ system.src.js:4597p @ system.src.js:4597h @ system.src.js:4597(anonymous function) @ system.src.js:4597run @ angular2-polyfills.js:138zoneBoundFn @ angular2-polyfills.js:111lib$es6$promise$$internal$$tryCatch @ angular2-polyfills.js:1511lib$es6$promise$$internal$$invokeCallback @ angular2-polyfills.js:1523lib$es6$promise$$internal$$publish @ angular2-polyfills.js:1494(anonymous function) @ angular2-polyfills.js:243run @ angular2-polyfills.js:138zoneBoundFn @ angular2-polyfills.js:111lib$es6$promise$asap$$flush @ angular2-polyfills.js:1305
system.src.js:4597 Uncaught Script error.
    Evaluating http://localhost/v010-commonsocialnetwork/assets/js/angular2/app/feed.component.jsU @ system.src.js:4597c.execute @ system.src.js:4597i @ system.src.js:4597s @ system.src.js:4597(anonymous function) @ system.src.js:4597(anonymous function) @ script.js:3(anonymous function) @ script.js:6U @ system.src.js:4597c.execute @ system.src.js:4597i @ system.src.js:4597n @ system.src.js:4597execute @ system.src.js:4597y @ system.src.js:4597w @ system.src.js:4597p @ system.src.js:4597h @ system.src.js:4597(anonymous function) @ system.src.js:4597run @ angular2-polyfills.js:138zoneBoundFn @ angular2-polyfills.js:111lib$es6$promise$$internal$$tryCatch @ angular2-polyfills.js:1511lib$es6$promise$$internal$$invokeCallback @ angular2-polyfills.js:1523lib$es6$promise$$internal$$publish @ angular2-polyfills.js:1494(anonymous function) @ angular2-polyfills.js:243run @ angular2-polyfills.js:138zoneBoundFn @ angular2-polyfills.js:111lib$es6$promise$asap$$flush @ angular2-polyfills.js:1305
angular2-polyfills.js:138 Script error.
    Evaluating http://localhost/v010-commonsocialnetwork/assets/js/angular2/app/script.js
    Error loading http://localhost/v010-commonsocialnetwork/assets/js/angular2/app/script.js

请让我知道我做错了什么以及如何解决这个问题。

提前致谢:)

【问题讨论】:

    标签: javascript angularjs typescript angular


    【解决方案1】:

    您使用的是哪个版本的 angular2?

    您似乎在混合使用 angular2 beta 和 angular2 rc 版本。

    在候选版本中:

    import { Component } from '@angular/core';
    

    和以前的版本

    import {bootstrap}    from 'angular2/platform/browser'
    

    如果您继续 rc,您可以使用:

    在候选版本中,您不应在脚本标签中添加角度库,
    相反,您应该在系统 js 配置中作为模块进行

    系统js配置示例

    System.config({
        transpiler: 'typescript',
        typescriptOptions: { emitDecoratorMetadata: true },
        map: {
            'app': 'app',
            'rxjs': 'libraries/rxjs',
            '@angular': 'libraries/@angular',
        },
        packages: {
            'app': { main: 'main.ts', defaultExtension: 'ts'},
            'rxjs': { main: 'Rx.js' },
            '@angular/core': { main: 'index.js' },
            '@angular/common': { main: 'index.js' },
            '@angular/compiler': { main: 'index.js' },
            '@angular/router': { main: 'index.js' },
            '@angular/platform-browser': { main: 'index.js' },
            '@angular/platform-browser-dynamic': { main: 'index.js' },
            '@angular/http': { main: 'index.js' },
        }
    });
    

    【讨论】:

    • 不使用angular js可以吗?
    • RC 这样做是对的,但是这里的代码引用的是 beta2.0。
    • 以及为什么在所有 main 中使用 index.js?
    • @EdmondWang 所以这个答案不是这个问题的直接答案,但可能对发布候选用户有用,我应该删除答案吗?
    • @asdf_enel_hak,我认为您可以编辑答案以推荐提问者将角度版本从 beta2.0 移动到 RC。
    【解决方案2】:

    我认为你可以替换

    import {FeedComponent} from 'assets/js/angular2/app/feed.component.js'
    

    import {FeedComponent} from 'assets/js/angular2/app/feed.component'
    

    为您在系统 js 配置中将默认文件扩展名指定为“ts”。 不确定这是否是根本原因。请先尝试一下。

    【讨论】:

    • 已经尝试过使用 .js 扩展名 .ts 扩展名和没有扩展名
    • 看来你使用的是beta2.0版本,不知道是不是这个问题:在组件装饰器中它被称为“templateUrl”而不是“template”。
    猜你喜欢
    • 2017-05-30
    • 1970-01-01
    • 2017-05-02
    • 2015-04-21
    • 1970-01-01
    • 1970-01-01
    • 2016-07-22
    • 2015-05-07
    • 2016-11-27
    相关资源
    最近更新 更多