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

时间:2016-12-19 09:24:16

标签: angular typescript visual-studio-2015 typescript2.0 angular-components

我来自angular 1背景,其中一切工作都非常顺利,甚至在使用angular 2的Visual Studio中使基本应用程序工作时也遇到了一些严重问题。我在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中将"Initiating Angular2.0"设置为启动文件并运行项目时,这会将输出显示为index.html。我的问题是,即使我将angularVersion变量更新为2.1或更改其他任何内容(从h2更改为p等)并重新运行该文件,它仍然会显示我"Initiating Angular2.0"。甚至硬编码Template也不会更新任何内容。我不知道自己做错了什么,但现在已经开始挫败了很多。有什么帮助吗?

2 个答案:

答案 0 :(得分:3)

在浏览器中按F12以显示开发人员工具。禁用缓存,如下所示

enter image description here

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

self.on_data()

来源:See here

答案 1 :(得分:0)

我改变了tsconfig.json

compileOnSavefalsetrue的值,对我有用。