Angular 2与客户端打字稿编译器

时间:2017-01-10 09:24:01

标签: angular typescript

我正在用打字稿学习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有问题吗?我的申请?

2 个答案:

答案 0 :(得分:0)

虽然我不使用System js,但似乎你没有激活热重新加载。如果你是初学者,Webpack更简单,我建议你去看看angular-cli。

无论如何here它说你必须使用hot reloader我认为它是什么,我从未使用过systemjs ......也可能是因为CPPUNIT_TEST((doTest<false,false>)); CPPUNIT_TEST((doTest<true,false>)); ,也许是为了标准设置(提示:angular-cli) ,或者像角度2 doc那样做,服务你的文件应该是cmd中的一个衬垫,当你刚开始时不需要wamp或任何奇怪的东西。

答案 1 :(得分:0)

修改end_location后,会重新编译,因为您可以在重新打开后看到更改。修改仅在您关闭并打开页面时有效,我认为其原因在于浏览器缓存。您可以尝试清除缓存并刷新。而且,重新编译需要一些时间,比如几秒钟。

自动编译由您使用的工具完成。我想你在启动测试服务器的脚本中有一些注意事项。也许您使用webpack或脚本(在官方入门教程中使用)。

如果您想自己编译typeScript代码,可以使用app.module.ts,它是npm package tsc的命令工具。

相关问题