2017-02-27 85 views
0

我在加載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:這是行不通的。起初,我認爲路徑有問題,但是當我嘗試不同的路徑時,我意識到這是另一回事。我想知道什麼會導致這些問題。

回答

1

您的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