Angularjs 2 - 更新模板或组件不会更新 UI

Angularjs 2 - Updating template or component does not update the UI

我来自 angular 1 背景,在那里一切工作都非常顺利,甚至在 Visual Studio 和 angular 2 中使基本应用程序工作时遇到一些严重问题。我在 visual studio 中创建了一个空项目,并非常仔细地遵循了“5 分钟教程”。它终于开始工作了,但是当我更改 app.component.ts 中的模板时,我发现更新 UI 时出现问题,浏览器上没有任何内容。我已重新启动并重新运行 文件,但浏览器上仍显示旧输出。

以下是我的代码:

tsconfig.json

{
  "compileOnSave": true,
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [ "es2015", "dom" ],
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true
  }
}

Systemjs.config.js:

/**
 * System configuration for Angular samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
  System.config({
    paths: {
      // paths serve as alias
      'npm:': 'node_modules/'
    },
    // map tells the System loader where to look for things
    map: {
      // our app is within the app folder
      app: 'app',

      // angular bundles
      '@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',

      // other libraries
      'rxjs':                      'npm:rxjs',
      'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js'
    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        main: './main.js',
        defaultExtension: 'js'
      },
      rxjs: {
        defaultExtension: 'js'
      }
    }
  });
})(this);

我的index.html有以下内容

<!DOCTYPE html>
<html>
  <head>
    <title>Angular QuickStart</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="styles.css">

      <!-- Polyfill(s) for older browsers -->
    <script src="node_modules/core-js/client/shim.min.js"></script>
    <script src="node_modules/zone.js/dist/zone.js"></script>
    <script src="node_modules/reflect-metadata/Reflect.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>

    <script src="systemjs.config.js"></script>
    <script>
      System.import('app').catch(function(err){ console.error(err); });
    </script>
  </head>

  <body>
    <my-app>Loading</my-app>
  </body>
</html>

app.module.ts,我有

import { NgModule }      from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { AppComponent }  from "./app.component";
@NgModule({
  imports:      [ BrowserModule ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})

export class AppModule { }

app.component.ts中:

import { Component } from "@angular/core";
@Component({
    selector: "my-app",
    template: "<h2>Initiating {{angularVersion}}</h2>"
})
export class AppComponent {
    angularVersion = "Angular2.0";
}

最后在 main.ts,我有:

import { platformBrowserDynamic } from "@angular/platform-browser-dynamic";
import { AppModule } from "./app.module";
platformBrowserDynamic().bootstrapModule(AppModule);

当我在 visual studio 2015 和 运行 项目中将 index.html 设置为启动文件时,这会将输出显示为 "Initiating Angular2.0"。我的问题是,即使我将 angularVersion 变量更新为 2.1 或更改任何其他内容(从 h2 到 p 等)并重新 运行 文件,它仍然会显示 "Initiating Angular2.0"。即使对 Template 进行硬编码也不会更新任何内容。我不知道我做错了什么,但现在开始很沮丧。有什么帮助吗?

在浏览器中按 F12 调出开发者工具。如下所示禁用缓存

其次,我不得不在 web.config 中的标记上方添加此部分以禁用缓存

<location path="app">
    <system.webServer>
      <staticContent>
        <clientCache cacheControlMode="DisableCache"/>
      </staticContent>
    </system.webServer>
</location>

来源:

我在 tsconfig.json 中更改了:

compileOnSave 值从 falsetrue,它对我有用。