【问题标题】:Angular 2 tutorial - part 4: "SyntaxError: Unexpected token <(…)"Angular 2 教程 - 第 4 部分:“SyntaxError: Unexpected token <(...)”
【发布时间】:2016-06-23 22:19:00
【问题描述】:

编辑:我尝试应用@Thierry 在他的回答中提供的修复程序,但是我一直收到同样的错误。 我创建了一个包含完整项目(没有 cmets 的干净)的存储库,因为它是按照教程并在应用 @Thierry 的修复后产生的:https://github.com/dragGH102/angular2-tutorial-part4-test

我在 https://angular.io/docs/ts/latest/tutorial/toh-pt4.html 关注 Angular 2 教程

在第 4 部分的结尾,我收到以下错误:

SyntaxError: Unexpected token

我什至尝试过:

  • 复制粘贴http://plnkr.co/edit/?p=preview 提供的 Plunkr 但同样的错误。
  • 删除 Promise 部分(根据下面的堆栈跟踪,这似乎是导致错误的原因)
  • 自己编译 TS 文件(而不是让它通过“npm start”来完成)

错误堆栈跟踪

SyntaxError: Unexpected token <(…)
Zone.run    @   angular2-polyfills.js:1243
zoneBoundFn @   angular2-polyfills.js:1220
lib$es6$promise$$internal$$tryCatch @   angular2-polyfills.js:468
lib$es6$promise$$internal$$invokeCallback   @   angular2-polyfills.js:480
lib$es6$promise$$internal$$publish  @   angular2-polyfills.js:451
lib$es6$promise$$internal$$publishRejection @   angular2-polyfills.js:401
(anonymous function)    @   angular2-polyfills.js:123
Zone.run    @   angular2-polyfills.js:1243
zoneBoundFn @   angular2-polyfills.js:1220
lib$es6$promise$asap$$flush @   angular2-polyfills.js:262

显然(例如 angular2: Uncaught SyntaxError: Unexpected token < )这是由于浏览器由于错误而找不到有效的 JS 代码,但我不知道出了什么问题。

这是我的代码(也可以在 http://plnkr.co/edit/Q5F0mV8Hbdcr2rtZUSw5 获得)

index.html

<html>
<head>
    <title>Angular 2 QuickStart</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="styles.css">

    <script src="node_modules/es6-shim/es6-shim.min.js"></script>
    <script src="node_modules/systemjs/dist/system-polyfills.js"></script>
    <script src="node_modules/angular2/es6/dev/src/testing/shims_for_IE.js"></script>

    <script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <script src="node_modules/rxjs/bundles/Rx.js"></script>
    <script src="node_modules/angular2/bundles/angular2.dev.js"></script>

    <script>
        System.config({
            transpiler: 'typescript',
            typescriptOptions: { emitDecoratorMetadata: true },
            packages: {'app': {defaultExtension: 'ts'}}
        });
        System.import('app/boot')
                .then(null, console.error.bind(console));
    </script>
</head>

<body>
    <my-app>Loading...</my-app>
</body>
</html>

app.component.ts

import {Component, OnInit} from 'angular2/core';
import {Hero} from './hero'; 
import {HeroDetailComponent} from './hero-detail.component'; 
import {HeroService} from './hero.service'; 

@Component({
    selector: 'my-app',
    template:`
      <h1>{{title}}</h1>
      <h2>My Heroes</h2>
      <ul class="heroes">
       <li *ngFor="#hero of heroes"
           [class.selected]="hero === selectedHero"
           (click)="onSelect(hero)">
         <span class="badge">{{hero.id}}</span> {{hero.name}}
       </li>
      </ul>
     <my-hero-detail [hero]="selectedHero"></my-hero-detail>
     `,
     // for the sake of code readibility I removed "styles"
    directives: [HeroDetailComponent], 
    providers: [HeroService], 
})

export class AppComponent implements OnInit {
    title = 'Tour of Heroes'; 
    heroes: Hero[]; 
    selectedHero: Hero; 

    constructor(private _heroService: HeroService) { } 

    getHeroes() {
        this.heroes = this._heroService.getHeroes().then(heroes => this.heroes = heroes);
    }

    ngOnInit() {
        this.getHeroes();
    }

    onSelect(hero: Hero) { this.selectedHero = hero; }

}

boot.ts

import {bootstrap}    from 'angular2/platform/browser' 
import {AppComponent} from './app.component'

bootstrap(AppComponent); 

hero-detail.component.ts

import {Component} from 'angular2/core';
import {Hero} from './hero';

@Component({
    selector: 'my-hero-detail',
    template: `
      <div *ngIf="hero">
        <h2>{{hero.name}} details!</h2>
        <div><label>id: </label>{{hero.id}}</div>
        <div>
          <label>name: </label>
          <input [(ngModel)]="hero.name" placeholder="name"/>
        </div>
      </div>`,
    inputs: ['hero']
})

export class HeroDetailComponent {
    hero: Hero;
}

hero.service.ts

import {Hero} from './hero';
import {HEROES} from './mock-heroes'; 
import {Injectable} from 'angular2/core';

@Injectable() 
export class HeroService { 
    getHeroes() {
        return Promise.resolve(HEROES); 
    }

}

hero.ts

export interface Hero {
    id: number;
    name: string;
}

mock-heroes.ts

import {Hero} from "./hero";

export var HEROES: Hero[] = [
    { "id": 11, "name": "Mr. Nice" },
    { "id": 12, "name": "Narco" },
    { "id": 13, "name": "Bombasto" },
    { "id": 14, "name": "Celeritas" },
    { "id": 15, "name": "Magneta" },
    { "id": 16, "name": "RubberMan" },
    { "id": 17, "name": "Dynama" },
    { "id": 18, "name": "Dr IQ" },
    { "id": 19, "name": "Magma" },
    { "id": 20, "name": "Tornado" }
];

【问题讨论】:

    标签: syntax-error angular


    【解决方案1】:

    我看了你的 plunkr,问题出在这行:

    getHeroes() {
        this.heroes = this._heroService.getHeroes().then(heroes => this.heroes = heroes);
    }
    

    实际上,您可以混合使用两种方法:

    • 要么在组件属性上设置承诺,然后在 async 管道上设置承诺解决时显示数据

      @Component({
        selector: 'my-app',
        template:`
          <li *ngFor="#hero of heroes">
            (...)
          </li>
        `
      })
      export class AppComponent implements OnInit {
        getHeroes() {
          this.heroes = this._heroService.getHeroes();
        }
      }
      
    • 您可以调用then 方法并在解析为组件属性时设置接收到的数据。在这种情况下,您不需要async 管道

      @Component({
        selector: 'my-app',
        template:`
          <li *ngFor="#hero of heroes">
            (...)
          </li>
        `
      })
      export class AppComponent implements OnInit {
        getHeroes() {
          this._heroService.getHeroes().then(heroes => this.heroes = heroes);
        }
      }
      

    在分叉的 plunkr 中查看我的更新:http://plnkr.co/edit/Rw4kOzKFbVosaoe7dRL1?p=preview

    否则我看不到像 SyntaxError: Unexpected token &lt;(…) 这样的错误,但大多数时候是因为您尝试加载 JS 文件并收到 404 错误...

    查看这个问题了解更多详情:

    【讨论】:

    猜你喜欢
    • 2017-05-30
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    • 2016-10-01
    • 2017-06-07
    相关资源
    最近更新 更多