【问题标题】:Typescript does not seem to work, no errors in code but component does not display打字稿似乎不起作用,代码中没有错误但组件不显示
【发布时间】:2016-05-04 09:08:43
【问题描述】:

我用 typescript 做了一个小型测试 angular2 应用程序。我没有任何错误,但我生成的 js 文件没有加载。这是我的代码;

在 default.aspx 页面我有我的选择器:

 <asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server">
 <app-main>Loading...</app-main>
 </asp:Content>

boot.ts

import {bootstrap} from 'angular2/platform/browser'
import {AppComponent} from './app.component'
bootstrap(AppComponent)

app.component.ts

import {Welcome} from './app.module'
import {Component} from 'angular2/core'
@Component({
selector: 'app-main',
template: `<h1>${Welcome.GetWelcome()}</h1>`
}) export class AppComponent { }

app.module.ts

export class Welcome {
static GetWelcome(): string {
    return "Hello World";
}
}

tsconfig.json

{
"compileOnSave": true,
"compilerOptions": {
"watch":  true,
"noImplicitAny": false,
"noEmitOnError": true,
"removeComments": false,
"sourceMap": true,
"target": "ES5",
"module": "system",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true
 },
 "exclude": [
 "./node_modules",
 "./typings/main",
 "./typings/main.d.ts"
 ]
 }

代码正在编译,但欢迎消息未加载到 app-main 中。我在这里做错了什么?我正在使用 Visual Studio 2015,版本 14.0

【问题讨论】:

  • 我很确定这不是在 angular2 的视图中绑定来自控制器的字符串的方式。 :)

标签: asp.net angularjs json typescript angular


【解决方案1】:

我为你做了一个 plunkr http://plnkr.co/edit/oMOvCgFzqtnYYPcE2U6j?p=preview

正如@toskv 所建议的那样,绑定字符串并不能正常工作。您将字符串定义为组件中的字段。并将其与模板中的双 {{msg}} 标签一起使用。这是简短的版本:

@Component({
selector: 'app-main',
template: `<h1>{{msg}}</h1>`
}) 
export class AppComponent {
  msg = Welcome.GetWelcome();      
}

【讨论】:

  • 感谢您为我制作了一个 plunker,但代码在我的 Visual Studio 版本中仍然无法运行,所以没有错误。
  • 是的,我已经实现了你的代码,但我的 Visual Studio 版本仍然无法工作
  • 不工作我的意思是 中的欢迎消息没有显示
  • @leokadia 您如何/在哪里配置 systemjs 以加载应用程序?您似乎在问题中忽略了这一点。或者也许你根本没有配置它并且代码没有加载? :P
  • 我有一个 tsconfig.json 文件
猜你喜欢
  • 2021-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-22
  • 2021-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多