[英]Angular 2 with client side typescript compiler
我正在用打字稿學習Angular 2。
我的第一步是使用客戶端打字稿編譯的簡單hello世界。 我也在使用wamp 3.0.4。
所以我有
的index.html
<!DOCTYPE html>
<html>
<head>
<title>Hello Angular</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {color:#369;font-family: Arial,Helvetica,sans-serif;}
</style>
<script src="https://unpkg.com/systemjs@0.19.31/dist/system.js"></script>
<script src="https://unpkg.com/reflect-metadata@0.1.3/Reflect.js"></script>
<script src="https://unpkg.com/zone.js/dist/zone.js"></script>
<script>
System.import('config.js').then( () => {
System.import('app')
}).catch( (err) => { console.error("System.import Error",err) })
</script>
</head>
<body>
<h1>Angular 2 with TypeScript</h1>
<my-app>Loading AppComponent content here ...</my-app>
</body>
</html>
config.js
System.config({
transpiler: 'typescript',
typescriptOptions: {
emitDecoratorMetadata: true
},
paths: {
'npm:': 'https://unpkg.com/'
},
map: {
'app': './app',
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router/bundles/router.umd.js',
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
'rxjs': 'npm:rxjs',
'typescript': 'npm:typescript@2.0.2/lib/typescript.js'
},
packages: {
app: {
main: './main.ts',
defaultExtension: 'ts'
}
});
main.ts
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
platformBrowserDynamic().bootstrapModule(AppModule)
和app.module.ts
import {Component,NgModule} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
@Component({
selector: 'my-app',
template: `
<div>
<h2>Hello {{name}}</h2>
</div>
`,
})
export class App {
name:string;
constructor() {
this.name = 'Angular2'
}
}
@NgModule({
imports: [ BrowserModule ],
declarations: [ App ],
bootstrap: [ App ]
})
export class AppModule {}
我不知道是否一切都是必要的,但......它正在發揮作用。
問題是如果我現在修改app.module.ts
,讓我們說<h2>Hello my name is {{name}}</h2>
,刷新網頁時不會顯示修改。 為了讓它工作,我必須關閉瀏覽器並再次打開頁面。 這就像編譯只進行一次並緩存。
這是一個問題與wamp? 我的申請?
修改app.module.ts
,會重新編譯它,因為您可以在重新打開后看到更改。 修改僅在您關閉並打開頁面時有效,我認為其原因在於瀏覽器緩存。 您可以嘗試清除緩存並刷新。 而且,重新編譯需要一些時間,比如幾秒鍾。
自動編譯由您使用的工具完成。 我想你在啟動測試服務器的腳本中有一些注意事項。 也許您使用webpack或腳本(在官方入門教程中使用)。
如果要自己編譯typeScript代碼,可以使用tsc
,它是npm包TypeScript
命令工具。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.