【问题标题】:Angularjs 2 - Updating template or component does not update the UIAngularjs 2 - 更新模板或组件不会更新 UI
【发布时间】:2017-05-04 07:32:34
【问题描述】:

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

【问题讨论】:

  • 你确定你的ts文件保存后是新编译的吗?您只需检查您的app.component.js 文件
  • 删除所有js(编译在app文件夹)文件试试
  • 奇怪的是,当我使用“npm start”运行它时,它可以工作!但是当我从 Visual Studio 中运行时,打字稿编译器不会生成更新的(编译的)javascript

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


【解决方案1】:

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

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

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

来源:See here

【讨论】:

  • 在生产模式下应该如何工作?我们不能一方面要求用户更改浏览器设置,另一方面仍想保持缓存。
【解决方案2】:

我换了tsconfig.json

compileOnSave 值从 falsetrue 对我有用。

【讨论】:

    猜你喜欢
    • 2019-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2012-12-31
    相关资源
    最近更新 更多