angular - Angularjs 2 - 更新模板或元件不會更新使用者介面
我來自 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 也不會更新任何內容。我完全不知道自己哪裡做錯了,但這已經讓我相當挫折了。有誰能幫忙嗎?
答案#1
在你的瀏覽器中按 F12 打開開發者工具。如下面所示禁用緩存
其次,我必須在 web.config 中的 標籤上方添加此部分以禁用緩存
<location path="app">
<system.webServer>
<staticContent>
<clientCache cacheControlMode="DisableCache"/>
</staticContent>
</system.webServer>
</location>
來源:見此
答案#2
我在 tsconfig.json 中更改了:
將 compileOnSave 的值從 false 改為 true,這對我有效。
