Tôi đang gặp khó khăn trong việc hiểu cách nhập các lớp từ Mô-đun trong TypeScript, đặc biệt cho Angular 2 trong Visual Studio 2015 (cập nhật 1) với TypeScript 1.7.Tăng gấp đôi với các lớp nhập từ mô-đun Angular 2 với Typescript 1.7
Mọi nơi trong tài liệu hướng dẫn 2 Tôi thấy các câu lệnh nhập như: import {Component} from 'angular2/core';
. Các tệp này nằm trong số node_modules/angular2/*
. Điều gì làm cho chỉ angular2/*
hoạt động?
Tôi chỉ có thể loại bỏ các lỗi trong Visual Studio khi tôi có đường dẫn tương đối từ thư mục ứng dụng như: ./../node_modules/angular2/platform/browser';
. Này sửa chữa các Visual Studio xây dựng sai sót, nhưng khi tôi cố gắng và chạy các ứng dụng với System.import('app/boot')
tôi nhận được một loạt các lỗi như thế này:
system.src.js:1049 GET http://localhost:8080/node_modules/angular2/platform/browser 404 (Not Found)
Một vấn đề khác là việc có thể sử dụng những câu như: import {SearchComponent} from './Components/Search/search.component';
trong Visual Studio, nhưng sau đó khi tôi chạy nó có rất nhiều lỗi GET tại system.src.js:2476
.
Tôi nghĩ rằng việc đặt defaultExtension: 'js'
cho System.config
nên đã giải quyết vấn đề đó.
CẬP NHẬT Dưới đây là tất cả các file mà tôi nghĩ là có liên quan:
views/home/index.html
<script src="node_modules/es6-shim/es6-shim.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>
System.config({
packages: {
app: {
format: 'register',
defaultExtension: 'js'
}
}
});
System.import('app/app')
.then(null, console.error.bind(console));
test.csproj
<TypeScriptToolsVersion>1.7</TypeScriptToolsVersion>
<TypeScriptExperimentalDecorators>True</TypeScriptExperimentalDecorators>
<TypeScriptModuleResolution>Node</TypeScriptModuleResolution>
<TypeScriptTarget>ES5</TypeScriptTarget>
<TypeScriptJSXEmit>None</TypeScriptJSXEmit>
<TypeScriptCompileOnSaveEnabled>True</TypeScriptCompileOnSaveEnabled>
<TypeScriptNoImplicitAny>False</TypeScriptNoImplicitAny>
<TypeScriptModuleKind>CommonJS</TypeScriptModuleKind>
<TypeScriptRemoveComments>False</TypeScriptRemoveComments>
<TypeScriptOutFile />
<TypeScriptOutDir />
<TypeScriptGeneratesDeclarations>False</TypeScriptGeneratesDeclarations>
<TypeScriptNoEmitOnError>True</TypeScriptNoEmitOnError>
<TypeScriptSourceMap>True</TypeScriptSourceMap>
<TypeScriptMapRoot />
<TypeScriptSourceRoot />
typings/tsd.d.ts
export * from './../node_modules/angular2/core';
export * from './../node_modules/angular2/common';
export * from './../node_modules/angular2/http';
export * from './../node_modules/angular2/router';
export * from './../node_modules/angular2/platform/browser';
cấu trúc File:
app/
app.ts
components/
models/
services/
node_modules/ (npm install using Angular 2 quickstart's package.json)
angular2/ (not all the files listed)
bundles/ (not all the files listed)
angular2.dev.js
platform/
src/
ts/
typings/
common.d.ts
core.d.ts
http.d.ts
router.d.ts
es6-module-loader/
es6-promise/
es6-shim/
rxjs/
systemjs/
zone.js/
typescript/ (not sure if this needs to be here)
Tôi không quen với nguyên cảo, có thể có được một lỗi gây ra bởi các hệ thống mô-đun nguyên cảo khác nhau? Angular 2 đề nghị System.config được thiết lập với format: 'register'
nhưng https://www.npmjs.com/package/angular2 nói rằng các tập tin có thể được tiêu thụ bằng cách sử dụng CommonJs.
Với những tập tin này, tôi nhận được hai giao diện điều khiển lỗi:
app.ts:1 Uncaught ReferenceError: require is not defined(anonymous function) @ app.ts:1
angular2-polyfills.js:143 Uncaught TypeError: Cannot read property 'split' of undefined
Có rất nhiều về thiết lập của bạn không được hiển thị ở đây, nhưng bên cạnh đó tôi không có cảm giác rằng bạn đang tải thư viện Angular 2 được xây dựng. Có vẻ như bạn vẫn đang cố tải các tệp nguồn chưa được tạo vào tài liệu của mình mà bạn không nên làm. Đó là 'bản đồ' kinh doanh bạn có ở trên là không cần thiết bởi vì như tôi đã nói trước khi SystemJS biết làm thế nào để tải các mô-đun chính xác ra khỏi thư viện được xây dựng. – pe8ter
Tôi cố gắng để thêm một số thông tin để làm cho nó như rõ ràng càng tốt. Tôi xin lỗi nếu tôi không mạch lạc. –
Bạn đã cài đặt kiểu gõ cho SystemJS bằng tsd chưa? – pe8ter