æåããå§ããŠãè¶ ã·ã³ãã«ãªAngular2 TypeScriptã¢ããªã±ãŒã·ã§ã³ãæ§ç¯ããŸãããã
ãã¢
Angular 2ã¢ããªã±ãŒã·ã§ã³ãã©ã®ããã«æ©èœãããã確èªããã«ã¯ã å®éã®äŸãå®è¡ããã®ãæåã®æ¹æ³ã§ãã
ãã®ãªã³ã¯ãã¯ãªãã¯ãããšãæ°ããã¿ããéãããµã³ãã«ãplunkerã«ããŒããããç°¡åãªã¡ãã»ãŒãžã衚瀺ãããŸãã
My First Angular 2 App
ãã¡ã€ã«æ§é ã¯æ¬¡ã®ãšããã§ãã
angular2-quickstart âââ app â âââ app.component.ts â âââ boot.ts âââ index.html âââ license.md
æ©èœçã«ã¯ã index.htmlãšapp /ãã©ã«ããŒå ã®2ã€ã®TypeScriptãã¡ã€ã«ã§ãã ãããŸãããïŒ
ãã¡ããããã©ã³ã«ãŒã§ã®ã¿å®è¡ãããã¢ããªã±ãŒã·ã§ã³ã¯äœæããŸããã å®ç掻ã§ããããã£ãŠãããã®ããã«ãã¹ãŠãããŸãããïŒ
- éçºç°å¢ãã»ããã¢ããããŸãã
- ã¢ããªã±ãŒã·ã§ã³ã®Angularã®ã«ãŒãã³ã³ããŒãã³ããäœæããŸãã
- ã¡ã€ã³WebããŒãžãå¶åŸ¡ã§ããããã«ããŠã³ããŒãããŠãã ããã
- ã¡ã€ã³ããŒãžïŒ index.html ïŒãæžããŸãããã
æ瀺ã«åŸã£ãŠãã¹ãŠã®ã³ã¡ã³ããç¡èŠããã°ã5åã§QuickStartãå®éã«æ§ç¯ã§ããŸãã
ããããç§ãã¡ã®å€ãã¯ãã®ãã¹ãŠãããªãããã©ã®ããã«ãèµ·ãããã«èå³ãæã¡ããããã®è³ªåãžã®çãã«ã¯ãã£ãšæéãããããŸãã
éçºç°å¢
å ŽæïŒãããžã§ã¯ããã©ã«ããŒïŒãããã€ãã®ã©ã€ãã©ãªãå€æ°ã®TypeScriptèšå®ãã奜ã¿ã®TypeScriptãµããŒããåãããšãã£ã¿ãŒãå¿ èŠã§ãã
ãããžã§ã¯ãçšã®æ°ãããã©ã«ããŒãäœæããŸãã
mkdir angular2-quickstart cd angular2-quickstart
å¿ èŠãªã©ã€ãã©ãªãè¿œå ããŸã
ã©ã€ãã©ãªã®ããŠã³ããŒããšç®¡çã«ã¯ãnpmããã±ãŒãžãããŒãžã£ãŒã䜿çšããããšããå§ãããŸãã
npmããããŸãããïŒ ãã®ãã¥ãŒããªã¢ã«å šäœã§æ°å䜿çšããããã ä»ããã€ã³ã¹ããŒã«ããŠãã ããã
package.jsonãã¡ã€ã«ããããžã§ã¯ããã©ã«ããŒã«è¿œå ãã次ã®ã³ãŒãããã®ãã©ã«ããŒã«ã³ããŒããŸãã
// 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.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": { "concurrently": "^1.0.0", "lite-server": "^1.3.1", "typescript": "^1.7.3" } }
詳现ãç¥ãããã§ããïŒ ç§ãã¡ã¯ããããèšå®ããŸãã 以äžã®ã¢ããªã±ãŒã·ã§ã³ ã
ããã±ãŒãžãã€ã³ã¹ããŒã«ããŸãã ã¿ãŒããã«ãŠã£ã³ããŠïŒWindowsã®ã³ãã³ãããã³ããïŒãéãã次ã®npmã³ãã³ããå®è¡ããŸãã
npm install
ã€ã³ã¹ããŒã«äžã«äžæ°å³ãªèµ€ããšã©ãŒã¡ãã»ãŒãžã衚瀺ãããå ŽåããããŸãã ããããç¡èŠããŸãã ã€ã³ã¹ããŒã«ã¯æåããŸãã èŠã 詳现ã«ã€ããŠã¯ã以äžã®ã¢ããªã±ãŒã·ã§ã³ãã芧ãã ããã
TypeScriptãæ§æãã
TypeScriptã³ã³ãã€ã©ãéåžžã«å ·äœçã«æ§æããå¿ èŠããããŸãã
tsconfig.jsonãã¡ã€ã«ããããžã§ã¯ããã©ã«ããŒã«è¿œå ãã以äžãã³ããŒããŸãã
// tsconfig.json { "compilerOptions": { "target": "ES5", "module": "system", "moduleResolution": "node", "sourceMap": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "removeComments": false, "noImplicitAny": false }, "exclude": [ "node_modules" ] }
tsconfig.jsonãæ¢çŽ¢ããŸã 以äžã®ã¢ããªã±ãŒã·ã§ã³ ã
ããã§ãã¹ãŠæºåãæŽããŸããã ããã€ãã®ã³ãŒããæžããŸãããã
æåã®Angularã³ã³ããŒãã³ã
ã³ã³ããŒãã³ãã¯Angularã®æãåºæ¬çãªæŠå¿µã§ãã ã³ã³ããŒãã³ãã¯ãã¥ãŒïŒãã¥ãŒïŒã調æŽããŸããããã¯ããŠãŒã¶ãŒã«æ å ±ã衚瀺ãããŠãŒã¶ãŒã®ã¢ã¯ã·ã§ã³ã«å¿çããWebããŒãžã®äžéšã§ãã
æè¡çã«ã¯ãã³ã³ããŒãã³ãã¯è¡šç€ºãã³ãã¬ãŒããå¶åŸ¡ããã¯ã©ã¹ã§ãã Angularã¢ããªã±ãŒã·ã§ã³ãäœæãããšãã«ããã®ãããªã¯ã©ã¹ãå€æ°äœæããŸãã ãããæåã®è©Šã¿ãªã®ã§ããã¹ãŠãéåžžã«ã·ã³ãã«ã«ããŸãã
ãœãŒã¹ãé 眮ããããµããã©ã«ããŒãäœæããŸã
ã¢ããªã±ãŒã·ã§ã³ã³ãŒããapp /ãšãããµããã©ã«ããŒã«ä¿åããããšããå§ãããŸãã ã³ã³ãœãŒã«ã§æ¬¡ã®ã³ãã³ããå®è¡ããŸãã
mkdir app cd app
ã³ã³ããŒãã³ããã¡ã€ã«ãè¿œå
ããã§ã app.component.tsãšãããã¡ã€ã«ãè¿œå ãã次ã®ã³ãŒãããã®ãã¡ã€ã«ã«ã³ããŒããŸãã
// app/app.component.ts import {Component} from 'angular2/core'; @Component({ selector: 'my-app', template: '<h1>My First Angular 2 App</h1>' }) export class AppComponent { }
ã¯ã©ã¹ãå®çŸ©ããæåŸããå§ããŠããã®ãã¡ã€ã«ã詳ããèŠãŠãããŸãããã
ã³ã³ããŒãã³ãã¯ã©ã¹
ãã¡ã€ã«ã®äžéšã«ã¯ã AppComponentãšåŒã°ãã空ã§å æãããŠããªãã¯ã©ã¹ããããŸãã ç¬ç«ããã¢ããªã±ãŒã·ã§ã³ãäœæããæºåãã§ãããããã®ã¯ã©ã¹ãã¢ããªã±ãŒã·ã§ã³ã®ããããã£ãšããžãã¯ã§è£å®ã§ããŸãã ãã®ã¯ã€ãã¯ã¹ã¿ãŒãã§ã¯äœãããå¿ èŠããªãããã AppComponentã¯ã©ã¹ã¯ç©ºã§ãã
ã¢ãžã¥ãŒã«
è§åºŠã¢ããªã±ãŒã·ã§ã³ã¯ã¢ãžã¥ãŒã«åŒã§ãã ãããã«ã¯å€ãã®ãã¡ã€ã«ãå«ãŸããŠãããããããã«ç¹å®ã®ç®çããããŸãã
ã»ãšãã©ã®ã¢ããªã±ãŒã·ã§ã³ãã¡ã€ã«ã¯ãã³ã³ããŒãã³ãã®ãããªãã®ããšã¯ã¹ããŒãããŸãã app.componentãã¡ã€ã«ã¯ã AppComponentã¯ã©ã¹ããšã¯ã¹ããŒãããŸãã
// app/app.component.ts (export) export class AppComponent { }
ãšã¯ã¹ããŒãã®äºå®ã¯ãéåžžã®ãã¡ã€ã«ãã¢ãžã¥ãŒã«ã«å€ããŸãã ãã¡ã€ã«åïŒæ¡åŒµåãªãïŒã¯éåžžãã¢ãžã¥ãŒã«ã®ååã§ãã ãããã£ãŠã ãapp.componentãã¯æåã®ã¢ãžã¥ãŒã«ã®ååã§ãã
ããè€éãªã¢ããªã±ãŒã·ã§ã³ã«ã¯ãããžã¥ã¢ã«ããªãŒã®AppComponentãç¶æ¿ããåã³ã³ããŒãã³ããããå ŽåããããŸãã ããè€éãªã¢ããªã±ãŒã·ã§ã³ã«ã¯ãå°ãªããšãã³ã³ããŒãã³ããšåãæ°ã®ãã¡ã€ã«ãšã¢ãžã¥ãŒã«ããããŸãã
QuickStartã¯è€éã§ã¯ãããŸããã1ã€ã®ã³ã³ããŒãã³ãã§ååã§ãã ãã ãããã®ãããªå°ããªã¢ããªã±ãŒã·ã§ã³ã§ããã¢ãžã¥ãŒã«ã¯åºæ¬çãªçµç¹äžã®åœ¹å²ãæãããŸãã
ã¢ãžã¥ãŒã«ã¯ä»ã®ã¢ãžã¥ãŒã«ã«äŸåããŠããŸãã Angular TypeScriptã¢ããªã±ãŒã·ã§ã³ã§ã¯ãå¥ã®ã¢ãžã¥ãŒã«ããæäŸããããã®ãå¿ èŠãªå Žåããããã€ã³ããŒãããŸãã å¥ã®ã¢ãžã¥ãŒã«ãAppComponentãåç §ããå¿ èŠãããå Žåã次ã®ããã«AppComponent ã·ã³ãã«ãã€ã³ããŒãããŸãã
// app/boot.ts import {AppComponent} from './app.component'
Angularã¯ã©ã€ãã©ãªã¢ãžã¥ãŒã«ã®ã³ã¬ã¯ã·ã§ã³ã§ããããŸãã åã©ã€ãã©ãªã¯ãå ±éã®æ¹åã«æ¥ç¶ãããè€æ°ã®ã¢ãžã¥ãŒã«ã§æ§æãããã¢ãžã¥ãŒã«ã§ãã
Angularããäœããå¿ èŠãªå Žåã¯ãã©ã€ãã©ãªã¢ãžã¥ãŒã«ããã€ã³ããŒãããŸãã ãããŠä»ãã³ã³ããŒãã³ãã®ã¡ã¿ããŒã¿ãå®çŸ©ã§ãããã®ãå¿ èŠã§ãã
ã³ã³ããŒãã³ãã®ã¡ã¿ããŒã¿
ã¯ã©ã¹ã¯ãã¡ã¿ããŒã¿ããã€ã³ããããšAngularã®ã³ã³ããŒãã³ãã«ãªããŸãã Angularã¯ããããã³ã°ã®æ§ç¯æ¹æ³ãããã³ã³ã³ããŒãã³ããã¢ããªã±ãŒã·ã§ã³ã®ä»ã®éšåãšå¯Ÿè©±ããæ¹æ³ãç解ããããã«ã¡ã¿ããŒã¿ãå¿ èŠãšããŸãã
Angularã®Componenté¢æ°ã䜿çšããŠã ã³ã³ããŒãã³ãã®ã¡ã¿ããŒã¿ãå®çŸ©ããŸãã ã¡ã€ã³ã®Angularã©ã€ãã©ãªã§ããangular2 / coreããã€ã³ããŒãããããšã§ããã®é¢æ°ã«ã¢ã¯ã»ã¹ããŸãã
// app/app.component.ts (import) import {Component} from 'angular2/core';
TypeScirptã§ã¯ãé¢æ°ãdecoratorã«å€æããããšã«ãããé¢æ°ãšã¯ã©ã¹ãé¢é£ä»ããããšãã§ããŸãã ãããè¡ãã«ã¯ãååã®åã«@èšå·ãè¿œå ããã¯ã©ã¹å®£èšã®çŽåã«é 眮ããŸãã
// app/app.component.ts (metadata) @Component({ selector: 'my-app', template: '<h1>My First Angular 2 App</h1>' })
@Componentã¯ããã®ã¯ã©ã¹ãAngularã®ã³ã³ããŒãã³ãã§ããããšãAngularã«äŒããŸãã @Componentã«éä¿¡ãããæ§æãªããžã§ã¯ãã«ã¯ã ã»ã¬ã¯ã¿ãŒãšãã³ãã¬ãŒãã® 2ã€ã®ãã£ãŒã«ãããããŸãã
selectorããããã£ã¯ããã¹ããšããŠæ©èœããHTMLèŠçŽ my-appã®éåžžã®CSSã»ã¬ã¯ã¿ãŒã決å®ããŸãã Angularã¯ã芪HTMLã§my-appãæ€åºãããã³ã«AppComponentã®ã€ã³ã¹ã¿ã³ã¹ãäœæããŠè¡šç€ºããŸãã
my-appã»ã¬ã¯ã¿ãŒãèŠããŠãããŠãã ããïŒ index.htmlãäœæãããšãã«ãã®æ å ±ãå¿ èŠã«ãªããŸãã
ãã³ãã¬ãŒãããããã£ã«ã¯ãã³ã³ããŒãã³ããã³ãã¬ãŒããå«ãŸããŠããŸãã ãã³ãã¬ãŒãã¯ããã£ã¹ãã¬ã€ã®ã¬ã³ããªã³ã°æ¹æ³ãAngularã«èª¬æããHTMLã®ããªãšãŒã·ã§ã³ã§ãã ãã®ãã³ãã¬ãŒãã¯ã ãMy First Angular Appãã宣èšããå¯äžã®HTMLè¡ã§ãã
ãã®ã³ã³ããŒãã³ããããŠã³ããŒãããå¿ èŠãããããšããAngularã«äœããã®åœ¢ã§èª¬æããå¿ èŠããããŸãã
å®è¡ãã
æ°ãããã¡ã€ã«boot.tsãapp /ãã©ã«ããŒã«è¿œå ãã次ã®ã³ãŒãããã®ãã¡ã€ã«ã«ã³ããŒããŸãã
// app/boot.ts import {bootstrap} from 'angular2/platform/browser' import {AppComponent} from './app.component' bootstrap(AppComponent);
ã¢ããªã±ãŒã·ã§ã³ãå®è¡ããã«ã¯ã2ã€ã®ããšãå¿ èŠã§ãã
- è§åºŠããŒãã¹ãã©ãããã©ãŠã¶æ©èœã
- äœæããã¢ããªã±ãŒã·ã§ã³ã®ã«ãŒãã³ã³ããŒãã³ãã
äž¡æ¹ãã€ã³ããŒãããŸãã 次ã«ã ããŒãã¹ãã©ãããåŒã³åºããŠã AppComponentã® ã«ãŒãã³ã³ããŒãã³ãã®åã æž¡ããŸãã
Angular2 / platform / browserããããŒãã¹ãã©ãããã€ã³ããŒãããçç±ãšãå¥ã®boot.tsãã¡ã€ã«ãäœæããçç±ã調ã¹ãããšãã§ããŸãã 以äžã®ã¢ããªã±ãŒã·ã§ã³ ã
ã³ã³ããŒãã³ããã«ãŒããšããŠãã©ãŠã¶ã§ã¢ããªã±ãŒã·ã§ã³ãå®è¡ããããã«Angularã«äŸé ŒããŸããã ãã ããAngularã¯ã©ãã§èµ·åããŸããïŒ
index.htmlããŒãžãè¿œå ããŸã
Angularã¯ã index.htmlããŒãžã®ç¹å®ã®å Žæã«ã¢ããªã±ãŒã·ã§ã³ã衚瀺ããŸãã ãã®ãã¡ã€ã«ãäœæããŸãã
index.htmlãapp /ãã©ã«ããŒã«å ¥ããŸããã ãããžã§ã¯ãã®ã«ãŒããã©ã«ããŒã®1ã€äžã®ã¬ãã«ã«é 眮ããŸã ã
cd ..
次ã«ã index.htmlãã¡ã€ã«ãäœæãã次ã®ãã¡ã€ã«ãã³ããŒããŸãã
<!-- index.html --> <html> <head> <title>Angular 2 QuickStart</title> <!-- 1. --> <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. SystemJS --> <script> System.config({ packages: { app: { format: 'register', defaultExtension: 'js' } } }); System.import('app/boot') .then(null, console.error.bind(console)); </script> </head> <!-- 3. --> <body> <my-app>Loading...</my-app> </body> </html>
泚æãã¹ãHTMLã®ã»ã¯ã·ã§ã³ã¯3ã€ãããŸãã
- å¿ èŠãªJavaScriptã©ã€ãã©ãªãããŒãããŠããŸãã Angular 2ãæ©èœãããã«ã¯ã angular2-polyfills.jsãšRx.jsãå¿ èŠã§ãã
- SystemãšåŒã°ãããã®ãã»ããã¢ããããäœæããããŒããã¡ã€ã«ãã€ã³ããŒãããããã«åœŒã«äŸé ŒããŸãã
- <my-app>ã¿ã°ã<body>ã«è¿œå ããŸãã ããã¯ãã¢ããªã±ãŒã·ã§ã³ãååšããå Žæã§ãïŒ
ã¢ããªã±ãŒã·ã§ã³ã®ã¢ãžã¥ãŒã«ãèŠã€ããŠããŠã³ããŒãããå¿ èŠããããŸãã ããã«ã¯SystemJSã䜿çšããŸãã å€ãã®ãªãã·ã§ã³ããããSystemJSãæè¯ã®éžæã§ãããšèšãããšã¯ã§ããŸããããç§ãã¡ã¯ãããæ°ã«å ¥ã£ãŠãããæ©èœããŠããŸãã
SystemJSæ§æã®è©³çŽ°ã¯ããã®ãã¥ãŒããªã¢ã«ã®ç¯å²å€ã§ãã æ§æã®äžéšã«ã€ããŠç°¡åã«èª¬æããŸã 以äžã®ã¢ããªã±ãŒã·ã§ã³ ã
Angularãboot.jsã§bootstrapé¢æ°ãåŒã³åºããšã AppComponentã¡ã¿ããŒã¿ãèªã¿åãã my-appã»ã¬ã¯ã¿ãŒãèŠã€ãã my-appãšããååã®ã¿ã°ãæ€åºããã¢ããªã±ãŒã·ã§ã³ããã®ã¿ã°ã«ããŒãããŸãã
ã³ã³ãã€ã«ããŠå®è¡ããŸãïŒ
ã¿ãŒããã«ãŠã£ã³ããŠãéãã次ã®ã³ãã³ããå ¥åããŸã
npm start
ãã®ã³ãã³ãã¯ã2ã€ã®äžŠåããŒãããã»ã¹ãéå§ããŸãã
- ç£èŠã¢ãŒãã®TypeScriptã³ã³ãã€ã©ã
- ãã©ãŠã¶ã«index.htmlãããŒãããã¢ããªã±ãŒã·ã§ã³ã³ãŒããå€æŽããããã³ã«ãã©ãŠã¶ãæŽæ°ããã©ã€ããµãŒããŒéçãµãŒããŒã
ãã°ãããããšããã©ãŠã¶ã®ã¿ããéããŠè¡šç€ºãããŸãïŒ
My First Angular 2 App
ããã§ãšãããããŸãïŒ ããªãã¯ä»äºäžã§ãïŒ
èªã¿èŸŒã¿äž...ã衚瀺ãããŠããå Žåã¯ããã¡ãããèªã¿ãã ãã ES6ãã©ãŠã¶ãµããŒãã¢ããªã±ãŒã·ã§ã³ ã
ããã€ãã®å€æŽãè¡ããŸã
ã¡ãã»ãŒãžããMy SECOND Angular 2 appãã«å€æŽããŠã¿ãŠãã ããã
TypeScriptã³ã³ãã€ã©ãšã©ã€ããµãŒããŒãã¢ã¯ã·ã§ã³ãç£èŠããŸãã å€æŽãæ€åºããTypeScriptãJavaScriptã«åã³ã³ãã€ã«ãããã©ãŠã¶ã¿ããæŽæ°ããŠãæŽæ°ãããã¡ãã»ãŒãžã衚瀺ããå¿ èŠããããŸãã
ããã¯ãã¢ããªã±ãŒã·ã§ã³ãéçºããããã®ãšã¬ã¬ã³ããªæ¹æ³ã§ãïŒ
ã³ã³ãã€ã©ãŒãšãµãŒããŒã®äž¡æ¹ãäžæããããã«ãã¹ãŠãè¡ã£ãããã¿ãŒããã«ãŠã£ã³ããŠãéããŸãã
æçµçãªæ§é
ãããžã§ã¯ããã©ã«ãã®æçµçãªæ§é ã¯æ¬¡ã®ããã«ãªããŸãã
angular2-quickstart âââ node_modules âââ app â âââ app.component.ts â âââ boot.ts âââ index.html âââ package.json âââ tsconfig.json
ãããŠãããã«ç§ãã¡ã®ãã¡ã€ã«ããããŸãïŒ
app / app.component.ts
import {Component} from 'angular2/core'; @Component({ selector: 'my-app', template: '<h1>My First Angular 2 App</h1>' }) export class AppComponent { }
app / boot.ts
import {bootstrap} from 'angular2/platform/browser' import {AppComponent} from './app.component' bootstrap(AppComponent);
index.html
<html> <head> <title>Angular 2 QuickStart</title> <!-- 1. --> <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. SystemJS --> <script> System.config({ packages: { app: { format: 'register', defaultExtension: 'js' } } }); System.import('app/boot') .then(null, console.error.bind(console)); </script> </head> <!-- 3. --> <body> <my-app>Loading...</my-app> </body> </html>
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.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": { "concurrently": "^1.0.0", "lite-server": "^1.3.1", "typescript": "^1.7.3" } }
tsconfig.json
{ "compileroptions": { "target": "es5", "module": "system", "moduleresolution": "node", "sourcemap": true, "emitdecoratormetadata": true, "experimentaldecorators": true, "removecomments": false, "noimplicitany": false }, "exclude": [ "node_modules" ] }
ãããã«
ç§ãã¡ã®æåã®ã¢ããªã±ãŒã·ã§ã³ã¯ããŸãããŸããã ããã¯åºæ¬çã«Angular 2ã®ãHello Worldãã§ãã
åããŠãå¯èœãªéããã¹ãŠãã·ã³ãã«ã«ããŸãããå°ããªAngularã³ã³ããŒãã³ããäœæããJavaScriptã©ã€ãã©ãªãindex.htmlã«è¿œå ããŠãéçãã¡ã€ã«ãµãŒããŒãèµ·åããŸããã å šäœãšããŠãHello Worldã¢ããªã«æåŸ ããã®ã¯ããã ãã§ãã
ãã£ãšæ·±å»ãªéå¿ããããŸã
è¯ããã¥ãŒã¹ã¯ãã€ã³ã¹ããŒã«ã«é¢ãã倧éšãã¯ç§ãã¡ã«é¢ä¿ãªããšããããšã§ãã ããããã package.jsonã«å°ã觊ããŠãã©ã€ãã©ãªãæŽæ°ããŸãã index.htmlã¯ãã©ã€ãã©ãªãŸãã¯cssã¹ã¿ã€ã«ã®ãã¡ã€ã«ãè¿œå ããå¿ èŠãããå Žåã«ã®ã¿éããŸãã
次ã®ã¹ããããžã®æºåãæŽããŸãããä»ãç§ãã¡ã®ã¿ã¹ã¯ã¯ãAngular 2ã§çŽ æŽãããããšãã§ããããšã瀺ãã¢ããªã±ãŒã·ã§ã³ãæ§ç¯ããããšã§ãã
Heroic Tourãã¥ãŒããªã¢ã«ã«åå ããŠãã ãã ïŒ
çšé
ãã®ç« ã®æ®ãã®éšåã§ã¯ãäžèšã§ç°¡åã«è§Šããããã€ãã®ãã€ã³ãããã詳现ã«èª¬æããäžé£ã®ã¢ããªã±ãŒã·ã§ã³ã«å°å¿µããŸãã
ããã«ã¯éèŠãªè³æã¯ãããŸããã 詳现ã«èå³ãããå Žåã¯èªãã§ãã ããã
âä»é²ïŒES6ãã©ãŠã¶ãŒã®ãµããŒã
Angular 2ã¯ES2015æšæºã®ããã€ãã®æ©èœã«äŸåããŠããããã®ã»ãšãã©ã¯æ¢ã«ææ°ã®ãã©ãŠã¶ãŒã«å«ãŸããŠããŸãã ãã ããäžéšã®ãã©ãŠã¶ãŒïŒIE11ãªã©ïŒã§ã¯ããã®æ©èœããµããŒãããããã«ããªãã£ã«ïŒã·ã ïŒãå¿ èŠã§ãã index.htmlã®ä»ã®ã¹ã¯ãªããã®åã« ã次ã®ããªãã£ã«ãããŒãããŠã¿ãŠãã ããã
<script src="node_modules/es6-shim/es6-shim.js"></script>
âä»é²ïŒpackage.json
npmã¯ããŒãçšã®äžè¬çãªããã±ãŒãžãããŒãžã£ãŒã§ãããå€ãã®Angularéçºè ã¯ããã䜿çšããŠãã¢ããªã±ãŒã·ã§ã³ã«å¿ èŠãªã©ã€ãã©ãªãããŠã³ããŒãããã³ç®¡çããŸãã
npm package.jsonãã¡ã€ã«ã§å¿ èŠãªããã±ãŒãžãç¹å®ããŸããã
AngularããŒã ã¯ããã®ãã¡ã€ã«ã®äŸåé¢ä¿ãšdevDependenciesã»ã¯ã·ã§ã³ã§æå®ãããããã±ãŒãžã䜿çšããããšãææ¡ããŠããŸãã
// package.json (dependencies) { "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": { "concurrently": "^1.0.0", "lite-server": "^1.3.1", "typescript": "^1.7.3" } }
ä»ã®ããã±ãŒãžãéžæã§ããŸãã ãã®ãããã¯ããã¹ãŠã®ã³ã³ããŒãã³ããé©åã«æ©èœããããšãããã£ãŠããããããå§ãããŸãã ä»ããç§ãã¡ã«åŸã£ãŠãã ããããããŠåŸã§ããªãã®åã³ã«å®éšããŠãããªãã®çµéšãšå³ã«åããªãã·ã§ã³ãéžæããŠãã ããã
ãªãã·ã§ã³ã®ã¹ã¯ãªããã»ã¯ã·ã§ã³ãpackage.jsonã«ååšããå ŽåããããŸãããã®ã»ã¯ã·ã§ã³ã§ã¯ãéçºãšæ§ç¯ãå®è¡ããããã®äŸ¿å©ãªã³ãã³ããå®çŸ©ã§ããŸãã æäŸããpackage.jsonã«ã¯ããã®ãããªã¹ã¯ãªãããããã€ãå«ãŸããŠããŸãã
// package.json (scripts) { "scripts": { "tsc": "tsc", "tsc:w": "tsc -w", "lite": "lite-server", "start": "concurrent \"npm run tsc:w\" \"npm run lite\" " } }
ãã®ã³ãã³ãã䜿çšããŠãã³ã³ãã€ã©ãŒãšãµãŒããŒãåæã«èµ·åããæ¹æ³ã«ã€ããŠã¯æ¢ã«èª¬æããŸããã
npm start
次ã®åœ¢åŒã§npmã¹ã¯ãªãããå®è¡ããŸãïŒ npm run + script-name ã ã¹ã¯ãªããã®æ©èœã®èª¬æã¯æ¬¡ã®ãšããã§ãã
- npm run tsc -1åã®å®è¡ã§TypeScriptã³ã³ãã€ã©ãå®è¡
- npm run tscïŒw -TypeScriptã³ã³ãã€ã©ãç£èŠã¢ãŒãã§èµ·åããŸãã ããã»ã¹ã¯åŒãç¶ãåäœããTypeScriptãã¡ã€ã«ã«å ããããå€æŽããã£ããã£ããæç¹ã§ãããžã§ã¯ããåã³ã³ãã€ã«ããŸãã
- npm run lite - lite-serverãèµ·åããŸããããã¯ã John Papaãäœæããã³ä¿å®ãã軜éã®éçãã¡ã€ã«ãµãŒããŒã§ãã«ãŒãã£ã³ã°ã䜿çšããAngularã¢ããªã±ãŒã·ã§ã³ã匷åã«ãµããŒãããŸãã
âä»é²ïŒnpmã®èŠåãšãšã©ãŒ
npm ERRã§å§ãŸãã³ã³ãœãŒã«ã¡ãã»ãŒãžãããå Žåã¯ãã¹ãŠæ£åžžã§ãïŒ npm install ã®æåŸã«ãããŸããã ã³ãã³ãã®å®è¡äžã«ããã€ãã®npm WARNã¡ãã»ãŒãžã衚瀺ãããå ŽåããããŸããããã¯çŽ æŽãããçµæã§ãã
gyp ERRã·ãªãŒãºã®åŸã«npm WARNã¡ãã»ãŒãžããã衚瀺ãããŸãïŒ åœŒãç¡èŠããŸãã ããã±ãŒãžã¯ã node-gypã䜿çšããŠèªåèªèº«ãåã³ã³ãã€ã«ããããšããå ŽåããããŸã ã ãã®è©Šè¡ã倱æããå Žåãããã±ãŒãžã¯ïŒéåžžã¯ä»¥åã®ããŒãžã§ã³ã«ïŒåŸ©å ããããã¹ãŠãæ©èœããŸãã
åäžã®npm ERRã¡ãã»ãŒãžããªãéãããã¹ãŠãæ£åžžã§ãïŒ npm installã®æåŸã«ã
âä»é²ïŒTypeScriptèšå®
æ§æãã¡ã€ã«ïŒ tsconfig.json ïŒããããžã§ã¯ãã«è¿œå ããŠãJavaScriptãã¡ã€ã«ãçæããæ¹æ³ãã³ã³ãã€ã©ãŒã«èª¬æããŸããã tsconfig.jsonã®è©³çŽ°ã«ã€ããŠã¯ãå ¬åŒã®TypeScript wikiã ã芧ãã ãã ã
ãã¡ã€ã«ã«è¿œå ãããªãã·ã§ã³ãšãã©ã°ãæãéèŠã§ãã
noImplicitAnyãã©ã°ã«ã€ããŠå°ã説æããããšæããŸãã TypeScriptéçºè ã¯ã trueãŸãã¯falseã«èšå®ããå¿ èŠããããã©ããã«åæããŸããã ããã«ã¯æ£ç¢ºãªçãã¯ãããŸããããã©ã°ã¯åŸã§ãã€ã§ãå€æŽã§ããŸãã ããããç§ãã¡ã®éžæã¯å€§èŠæš¡ãããžã§ã¯ãã«æ·±å»ãªåœ±é¿ãäžããå¯èœæ§ããããããè°è«ã«å€ããŸãã
noImplicitAnyã falseã«èšå®ãããŠããå Žåãã³ã³ãã€ã©ãŒã¯ãå€æ°ã®äœ¿çšæ¹æ³ã«å¿ããŠå€æ°ã®åãæšæž¬ã§ããªãå Žåãå€æ°ã®åãå¯ãã«anyã«èšå®ããŸã ã ããã¯ããæé»ã®ãããã ããæå³ããŸãã
noImplicitAnyã trueã«èšå®ãããTypeScriptã³ã³ãã€ã©ãŒãåãæšæž¬ã§ããªãå ŽåãJavaScriptãã¡ã€ã«ã¯çæãããŸããããšã©ãŒãå ±åãããŸãã
ãã®ã¯ã€ãã¯ã¹ã¿ãŒãããã³ãã®éçºè ã¬ã€ãã®ä»ã®å€ãã®äŸã§ã¯ã noImplicitAnyãfalseã«èšå®ããŸã ã
ãã匷åãªã¿ã€ãã³ã°ã奜ãéçºè ã¯ã noImplicitAnyãtrueã«èšå®ããå¿ èŠããããŸã ã å€æ°ã®åãæé©ãªéžæãšæãããå Žåã¯ä»»æã«èšå®ã§ããŸããããã®ã¹ãããã®å¿ èŠæ§ãå°ãåæ ããåŸã«æ瀺çã«è¡ãå¿ èŠããããŸãã
noImplicitAnyãtrueã«èšå®ãããšãé衚瀺ã®ã€ã³ããã¯ã¹ãšã©ãŒãçºçããŸãã ãããæ¯æŽãããåä»ã§ãããšæãããå Žåã¯ã次ã®ãã©ã°ã䜿çšããŠãããããªãã«ããããšãã§ããŸãã
"suppressImplicitAnyIndexErrors":true
âä»é²ïŒSystemJSèšå®
QuickStartã¯SystemJSã䜿çšããŠãã¢ããªã±ãŒã·ã§ã³ãšã©ã€ãã©ãªã¢ãžã¥ãŒã«ãããŠã³ããŒãããŸãã ãã ããé«ãè©äŸ¡ãããŠããã³ãã¥ããã£webpackãªã©ã®ä»£æ¿æ©èœãããããšãå¿ããªãã§ãã ããã SystemJSã¯è¯ãéžæã§ãããããã¯åãªãéžæã§ããã奜ã¿ã§ã¯ãªãããšãæ確ã«ããããšæããŸãã
ãã¹ãŠã®ã¢ãžã¥ãŒã«ããŒããŒã«ã¯æ§æãå¿ èŠã§ãããããŒããŒã®æ§æã¯ããè€éã«ãªãããã¡ã€ã«æ§é ãããå€æ§ã«ãªããŸã-ããã©ãŒãã³ã¹ãåäžãããããã«ãã¡ã€ã«ãçµåããããšãèãå§ãããšãããŸã§ã
éžæããããŒãããŒããŒãããç解ããããšããå§ãããŸãã
SystemJSèšå®ã®è©³çŽ°ã«ã€ããŠã¯ã ãã¡ããã芧ãã ããã
ãããã®èŠåãèãããšãç§ãã¡ã¯äœãã§ããŸããïŒ
<!-- index.html ( SystemJS) --> <script> System.config({ packages: { app: { format: 'register', defaultExtension: 'js' } } }); System.import('app/boot') .then(null, console.error.bind(console)); </script>
packagesããŒãã¯ã ã¢ããªã±ãŒã·ã§ã³/ãã©ã«ããŒããã®ã¢ãžã¥ãŒã«èŠæ±ãæ€åºããå Žåã®åŠçââãSystemJSã«æ瀺ããŸãã
ã¯ã€ãã¯ã¹ã¿ãŒãã¯ãTypeScriptã¢ããªã±ãŒã·ã§ã³ãã¡ã€ã«ã§åæ§ã®ã€ã³ããŒãã¹ããŒãã¡ã³ããèŠã€ãããã³ã«ããã®ãããªãªã¯ãšã¹ããäœæããŸãã
// boot.ts () import {AppComponent} from './app.component'
ã¢ãžã¥ãŒã«åïŒ ãã ïŒã«ã¯ãã¡ã€ã«æ¡åŒµåãå«ãŸããŠããªãããšã«æ³šæããŠãã ããã ããã±ãŒãžæ§æã§ã¯ãããã©ã«ãã§SystemJSæ¡åŒµæ©èœããjsããã€ãŸãJavaScriptãã¡ã€ã«ãšããŠå®çŸ©ããŸãã
ããã¯ãã¢ããªã±ãŒã·ã§ã³ãèµ·åããåã«JavaScriptã§TypeScriptãã³ã³ãã€ã«ãããããåççã§ãã
plunkerãã¢ã§ã¯ããã©ãŠã¶ãŒã§JavaScriptãçŽæ¥ãªã³ã¶ãã©ã€ã§ã³ã³ãã€ã«ããŸãã ããã¯ãã¢ã«ã¯æªãããšã§ã¯ãããŸããããéçºããªãªãŒã¹ã«é©ããŠãããšã¯éããŸããã
ããã€ãã®çç±ã«ãããã¢ããªã±ãŒã·ã§ã³ãèµ·åããåã®ãã«ããã§ãŒãºã§JavaScriptãã³ã³ãã€ã«ããããšããå§ãããŸãã
- ãã©ãŠã¶ãŒã§é衚瀺ã«ãªã£ãŠããã³ã³ãã€ã«æéã®ãšã©ãŒãšèŠåã確èªã§ããŸãã
- , , .
- , .
- , . , .
- â , , .
ã³ãŒã«System.import㯠SystemJSã¯ããã¡ã€ã«ãã€ã³ããŒãããŸããªãããŒãïŒboot.js ...ã³ã³ãã€ã«åŸboot.tsïŒãèŠããŠãããŠãã ããïŒæªããŒã -ããã¯æã ãè§åºŠã®å®è¡ã¢ããªã±ãŒã·ã§ã³ãå°ãããã¡ã€ã«ã§ãããŸããèµ·åãšã©ãŒããã£ããããŠã³ã³ãœãŒã«ã«èšé²ããŸãã
ä»ã®ãã¹ãŠã®ã¢ãžã¥ãŒã«ã¯ãimportã¹ããŒãã¡ã³ããŸãã¯Angularèªäœã«ãã£ãŠäœæãããã¯ãšãªã䜿çšããŠããŒããããŸãã
âã¢ããªã±ãŒã·ã§ã³ïŒboot.ts
ãã©ãããã©ãŒã èªã¿èŸŒã¿åºæ
ããŒãã¹ãã©ããé¢æ°ã¯ãangular2 / coreããã§ã¯ãªããangular2 / platform / browserããã€ã³ããŒãããŸããããã«ã¯çç±ããããŸãããã¹ãŠã®ã¿ãŒã²ãããã©ãããã©ãŒã ã§åãæ©èœã®ã¿ããã³ã¢ããšåŒã¶ããšãã§ããŸããå®éãå€ãã®Angularã¢ããªã±ãŒã·ã§ã³ã¯ãã©ãŠã¶ã§ããå®è¡ã§ããããã®ã©ã€ãã©ãªããæé·æéããŒãã¹ãã©ããé¢æ°ãåŒã³åºããŸãããã©ãŠã¶å°çšã«äœæããå Žåãããã¯ãã³ã¢ãæ©èœã§ããããããå¥ã®ç°å¢ã§ã³ã³ããŒãã³ããããŒãããããšã¯ããªãå¯èœã§ããApache Cordovaã䜿çšããŠã¢ãã€ã«ããã€ã¹ã«ããŠã³ããŒãã§ããŸãããµãŒããŒäžã§ã¢ããªã±ãŒã·ã§ã³ã®ããŒã ããŒãžãã¬ã³ããªã³ã°ããŠãçç£æ§ãåäžããããã
SEOããã¢ãŒã·ã§ã³ã
ãããã®ãã©ãããã©ãŒã ã«ã¯ãå¥ã®ã©ã€ãã©ãªããããŒãããããŒãã¹ãã©ããæ©èœã®ããã®ãã®ä»ã®ãªãã·ã§ã³ãå¿ èŠã§ãã
å¥ã®boot.tsãã¡ã€ã«ãäœæããçç±
boot.tsãã¡ã€ã«ã¯å°ããã§ããããã¯åãªãã¯ã€ãã¯ã¹ã¿ãŒãã§ããapp.componentãã¡ã€ã«ã«ãããã®æ°è¡ãå ¥åããã ãã§ãäžå¿ èŠãªè€éããã解æŸãããŸãã
ãã ããéèŠã ãšæãããçç±ã§ãããè¡ããŸããã
- æ£ããè¡ãã®ã¯ç°¡åã§ãã
- ãã¹ãã®ããããã
- åå©çšã®äŸ¿å©ãã
- è·åã®åé¢ã
- ã€ã³ããŒããšãšã¯ã¹ããŒãã®å匷ã
ãã
ã¯ããããã¯è¿œå ã®æé ããã³è¿œå ã®ãã¡ã€ã«ã§ããå šäœçã«ã©ã®ãããé£ããã§ããïŒQuickStartã«ãšã£ãŠããã»ã©éèŠã§ã¯ãªãå Žåã§ããã»ãšãã©ã®ã¢ããªã±ãŒã·ã§ã³ã§ã¯
å¥ã®boot.tsãã¡ã€ã«ãæãŸããããšãããããŸããäœäŸ¡æ Œãªãããä»ããè¯ãç¿æ £ã身ã«ä»ããŸãããã
ãŠãŒã¶ããªãã£ãã¹ã
QuickStartã決ããŠãã¹ãããªãããšãããã£ãŠããŠããæåãããã¹ãã®å©äŸ¿æ§ã«ã€ããŠèããå¿ èŠããããŸããããŒãã¹ãã©ãã
æ©èœãåããã¡ã€ã«ã«ååšããå Žåãã³ã³ããŒãã³ãããã¹ãããããšã¯å°é£ã§ãããã¹ãçšã®ã³ã³ããŒãã³ããã¡ã€ã«ãããŠã³ããŒããããã³ã«ãããŒãã¹ãã©ããæ©èœã¯ã¢ããªã±ãŒã·ã§ã³ããã©ãŠã¶ãŒã«ããŒãããããšããŸããããã¯ãã¢ããªã±ãŒã·ã§ã³å šäœãèµ·åããããšãæ³å®ããŠããªãã£ãããããšã©ãŒãåŒãèµ·ãããŸããã³ã³ããŒãã³ãã ãã§ãã移åããŒãã¹ãã©ããã§-functionãboot.tsããããšã¯èª€ã£ããšã©ãŒãšçŽç²ãªã³ã³ããŒãã³ããã¡ã€ã«ãšèãã¡ãåé€ããŸãã
åå©çš
ã¢ããªã±ãŒã·ã§ã³ã®é²åãéããŠããã¡ã€ã«ããªãã¡ã¯ã¿ãªã³ã°ãååå€æŽãããã³ç§»åããŸãããã¡ã€ã«ãbootstrapãåŒã³åºããŸã§ããããè¡ãããšã¯ã§ããŸããã移åã§ããŸãããå¥ã®ã¢ããªã±ãŒã·ã§ã³ã§ã³ã³ããŒãã³ããåå©çšããããšã¯ã§ããŸããããµãŒããŒã«ã³ã³ããŒãã³ããã¬ã³ããªã³ã°ããŠããã©ãŒãã³ã¹ãåäžãããããšã¯ã§ããŸããã
è·ååé¢
ã³ã³ããŒãã³ãã®ã¿ã¹ã¯ã¯ã衚瀺ãè¡šçŸããã³ç®¡çããããšã§ãã
ã¢ããªã±ãŒã·ã§ã³ã®å®è¡ã¯ã衚瀺å¶åŸ¡ãšã¯é¢ä¿ãããŸãããããã¯å®å šã«ç°ãªã矩åã§ãããã¹ããšåå©çšäžã«çºçããåé¡ã¯ããã®äžå¿ èŠãªè²¬ä»»ã®æ··ä¹±ã«èµ·å ããŠããŸãã
ã€ã³ããŒã/ãšã¯ã¹ããŒã
å¥ã®boot.tsãã¡ã€ã«ãäœæãããšããAngularãæäœããããã®éèŠãªã¹ãã«ãåŸãããŸãããããã¢ãžã¥ãŒã«ããäœãããšã¯ã¹ããŒãããŠå¥ã®ã¢ãžã¥ãŒã«ã«ã€ã³ããŒãããæ¹æ³ã§ããAngularãšããç·å¯ã«é£æºãããšãã«ããã®å€ããè¡ããŸãã