ASP MVC项目中的角度:模板不显示

时间:2018-10-16 13:50:23

标签: asp.net-mvc angular

我是angular的新手,并按照youtube-tutorial的介绍如何在asp mvc项目中使用它。一切正常,我得到了以下结构:

enter image description here

然后我在app.component.ts上工作,它看起来像这样:

import { Component } from '@angular/core';

@Component({
  selector: 'chtest',
  template: `<h1>Hello {{name}}</h1>`,
})
export class AppComponent
{
    constructor() { }

    name = 'Angular';
}

我还使用templateUrl(和app.component.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
{
    constructor() { }
}

然后我创建了一个仅包含以下内容的简单视图:

<chtest></chtest>

运行应用程序时,没有显示任何内容。我希望它显示“ Hello Angular”,但我想我在这里缺少什么?

就像我说的那样,像node_modules这样的文件夹就在那儿,youtube教程有效(https://www.youtube.com/watch?v=rbHSTJBhJ44

我正在运行Visual Studio Professional 2017,v15.8.4

提前谢谢!

编辑:

我尝试了w3schools的其他方法:

My first expression: {{ 5 + 5 }}

除了

enter image description here

1 个答案:

答案 0 :(得分:0)

要引导Angular应用程序,您需要调用:

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'

platformBrowserDynamic().bootstrapModule(AppModule);

应用程序将被加载到<app-root></app-root>选择器中,因此请将其添加到您的Index.cshtml文件中。

此后,您的组件将被引导到<app-root></app-root>