无法在mvc中加载angular2 templateUrl 5

问题描述:

我在加载angular2应用程序中的模板时遇到了问题。无法在mvc中加载angular2 templateUrl 5

system.config.js:

(function (global) { 
    System.config({ 
     paths: { 
      // paths serve as alias 
      'npm:': '/libs/' 
     }, 
     // map tells the System loader where to look for things 
     map: { 
      // our app is within the app folder 
      app: '/Scripts', 
      // 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', 
      '@angular/material': 'npm:@angular/material/bundles/material.umd.js', 
      // other libraries 
      'rxjs': 'npm:rxjs', 
      'angular2-in-memory-web-api': 'npm:angular2-in-memory-web-api', 
     }, 
     // 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' 
      }, 
      'angular2-in-memory-web-api': { 
       main: './index.js', 
       defaultExtension: 'js' 
      } 
     } 
    }); 
})(this); 

boot.ts

import { NgModule } from '@angular/core'; 
import { BrowserModule } from '@angular/platform-browser'; 

import { AccountsAppComponent } from './Accounts/app'; 
import { LoginComponent } from './Accounts/Login/Login.component'; 
import { RegisterComponent } from './Accounts/Register/Register.component'; 

@NgModule({ 
    imports: [ 
     BrowserModule 
    ], 
    declarations: [ 
     AccountsAppComponent, 
     LoginComponent, 
     RegisterComponent 
     ], 
    bootstrap: [AccountsAppComponent] 
}) 
export class AppModule { } 

app.ts:

import { Component } from '@angular/core'; 
import { LoginComponent } from './Login/Login.component'; 
import { RegisterComponent } from './Register/Register.component'; 

@Component({ 
    selector: 'my-app', 
    template: `<login-form></login-form> <register-form></register-form>` 
}) 
export class AccountsAppComponent { 
    title = 'ASP.NET MVC 5 with Angular 2'; 
    skills = ['MVC 5', 'Angular 2', 'TypeScript', 'Visual Studio 2015']; 
    myskills = this.skills[1]; 
} 

Register.component.ts:

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

@Component({ 
    selector: 'register-form', 
    templateUrl: './Accounts/Register/Register.component.html' 
}) 
export class RegisterComponent implements OnInit { 
    constructor() { } 

    ngOnInit() { } 
} 

我曾尝试不同的路径,如:

'./Accounts/Register/Register.component.html' 
'./Register/Register.component.html' 
'./Register.component.html' 

如果我使用template:标签,我可以成功地加载我的HTML代码,但是如果我使用templateUrl:这是行不通的。起初,我认为路径有问题,但是当我尝试不同的路径时,我意识到这是另一回事。我想知道什么会导致这些问题。

您的templateUrl的相对路径不正确。 ./是相对于当前文件夹,因此它正在寻找<root>/Accounts/Register/Accounts/Register/Register.component.html

更改您的路径是相对于文件的位置。最有可能你的模板或它的位置的文件名不匹配,但正确的路径应该是:

'./Register.component.html' 

假设你的项目结构是:

<root> 
    boot.ts 
    <Accounts> 
     app.ts 
     <Register> 
      Register.component.html 
      Register.component.ts 
     <Login> 
      Login.component.ts