2016-02-01 4 views
3

Я начал простое приложение Angular 2, все работало. Но когда я добавляю импорт lodash и пытаюсь его использовать, я получаю ошибки, и приложение перестает работать, и не может понять, в чем проблема.angular2 сбой lodash импорт

Я получаю эти 2 ошибки в консоли:

Uncaught SyntaxError: Unexpected token <__exec @ system.src.js:1374entry.execute @ system.src.js:3300linkDynamicModule @ system.src.js:2921link @ system.src.js:2764execute @ system.src.js:3096doDynamicExecute @ system.src.js:715link @ system.src.js:908doLink @ system.src.js:569updateLinkSetOnLoad @ system.src.js:617(anonymous function) @ system.src.js:430run @ 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

Uncaught SyntaxError: Unexpected token < Evaluating http://localhost:3000/lodash Error loading http://localhost:3000/app/app.jsrun @ 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:1494lib$es6$promise$$internal$$publishRejection @ angular2-polyfills.js:1444(anonymous function) @ angular2-polyfills.js:243run @ angular2-polyfills.js:138zoneBoundFn @ angular2-polyfills.js:111lib$es6$promise$asap$$flush @ angular2-polyfills.js:1305

FILES

index.html

<html> 
 

 
    <head> 
 
    <title>Angular 2 QuickStart</title> 
 

 
    <!-- 1. Load libraries --> 
 
    <!-- IE required polyfills, in this exact order --> 
 
    <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/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> 
 

 
    <!-- 2. Configure SystemJS --> 
 
    <script> 
 
     System.config({ 
 
     packages: { 
 
      app: { 
 
      format: 'register', 
 
      defaultExtension: 'js' 
 
      } 
 
     } 
 
     }); 
 
     System.import('app/app') 
 
      .then(null, console.error.bind(console)); 
 
    </script> 
 

 
    </head> 
 

 
    <!-- 3. Display the application --> 
 
    <body> 
 
    <my-app>Loading...</my-app> 
 
    </body> 
 

 
</html>

tsconfig.json

{ 
 
    "compilerOptions": { 
 
    "target": "es5", 
 
    "module": "system", 
 
    "moduleResolution": "node", 
 
    "sourceMap": true, 
 
    "emitDecoratorMetadata": true, 
 
    "experimentalDecorators": true, 
 
    "removeComments": false, 
 
    "noImplicitAny": false 
 
    }, 
 
    "exclude": [ 
 
    "node_modules" 
 
    ] 
 
}

package.json

{ 
 
    "name": "angular2-quickstart", 
 
    "version": "1.0.0", 
 
    "scripts": { 
 
    "tsc": "tsc", 
 
    "tsc:w": "tsc -w", 
 
    "lite": "lite-server", 
 
    "start": "concurrent \"npm run tsc:w\" \"npm run lite\" " 
 
    }, 
 
    "license": "ISC", 
 
    "dependencies": { 
 
    "angular2": "2.0.0-beta.2", 
 
    "es6-promise": "^3.0.2", 
 
    "es6-shim": "^0.33.3", 
 
    "lodash": "^4.1.0", 
 
    "reflect-metadata": "0.1.2", 
 
    "rxjs": "5.0.0-beta.0", 
 
    "systemjs": "0.19.6", 
 
    "zone.js": "0.5.10" 
 
    }, 
 
    "devDependencies": { 
 
    "concurrently": "^1.0.0", 
 
    "lite-server": "^1.3.4", 
 
    "typescript": "^1.7.5" 
 
    } 
 
}

import {bootstrap} from 'angular2/platform/browser' 
 
import {Component} from 'angular2/core'; 
 
import * as _ from 'lodash'; 
 

 
@Component({ 
 
    selector: 'my-app', 
 
    template: '<h1>My First Angular 2 App with lodash</h1>' 
 
}) 
 
export class AppComponent { 
 

 
    constructor(){ 
 
     console.log(_.last([1, 2, 3])); 
 
    \t console.log('hello'); 
 
    } 
 

 
} 
 

 
bootstrap(AppComponent);

+0

проверить, если у вас установлен пакет lodash НПМ , – Jai

+0

milan @ milan-desktop: ~/Desktop/newapp $ npm install -S lodash npm WARN package.json [email protected] Нет описания npm WARN package.json [email protected] Нет поля репозитория. npm WARN package.json [email protected] Нет данных README [email protected] node_modules/lodash –

+0

Я вижу lodash в node_modules и в package.json, а все вроде нормально ... –

ответ

14

Может быть, вы могли бы попробовать эту конфигурацию на уровне SystemJS:

System.config({ 
    (...) 
    map: { 
    lodash: 'node_modules/lodash/lodash.js' 
    }, 
    meta: { 
    lodash: { format: 'amd' } 
    } 
} 

В файле TS, то вы можете использовать его, как описано ниже:

import _ from 'lodash'; 
_.forEach([1,2,3], function(e) { 
    console.log(e); 
}); 

Этот вопрос может помочь вам: https://github.com/systemjs/systemjs/issues/951.

Thierry

+0

Мне просто нужно добавить часть «map» в system.config, и теперь она работает! Большое спасибо! –

2

Полностью рабочая:

Установить все через терминал:

npm install lodash --save 
tsd install lodash --save 

Добавить пути в index.html

<script> 
    System.config({ 
     packages: { 
      app: { 
       format: 'register', 
       defaultExtension: 'js' 
      } 
     }, 
     paths: { 
      lodash: './node_modules/lodash/lodash.js' 
     } 
    }); 
    System.import('app/init').then(null, console.error.bind(console)); 
</script> 

Импорт lodash в верхней части .ts файл

import * as _ from 'lodash' 
+1

tsd лишен, вместо этого нужно использовать тиски –

0

Я решил импортировать lodash следующим образом:

import * as _ from 'lodash';

и systemjs.config.js я определил это:

map: { 'lodash' : 'node_modules/lodash/lodash.js' }

Смежные вопросы