【发布时间】: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