MVC 5 Angular 2アプリを作成しました。しかし、読み込み後、別のページ(Aboutなど)に移動すると、セレクタ "my-app"が要素と一致しないという例外が発生します。MVC + Angular 2アプリでMVCルーティングを使用している場合の例外
フォルダ構造:ここでは、関連するすべてのコードです
| - App
| | - app.ts
| | - boot.ts
| | - main.ts
|
| - Views
| | - Shared
| | - _Layout.cshtml
|
| - systemjs.config.js
_Layout.cshtml
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - My ASP.NET Application</title>
<!-- Polyfill(s) for older browsers -->
<script src="~/node_modules/core-js/client/shim.min.js"></script>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
@Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
</div>
</div>
</div>
<div class="container body-content">
@RenderBody()
<hr />
<footer>
<p>© @DateTime.Now.Year - My ASP.NET Application</p>
</footer>
</div>
<script src="~/node_modules/zone.js/dist/zone.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>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
</body>
</html>
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);
main.ts
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './boot';
const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule);
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app';
@NgModule({
imports: [BrowserModule],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule { }
app.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<h2>My favorite skill is: {{myskills}}</h2>
<p>Skill:</p>
<ul>
<li *ngFor="let skl of skills">
{{ skl }}
</li>
</ul>
`
})
export class AppComponent {
title = 'ASP.NET MVC 5 with Angular 2';
skills = ['MVC 5', 'Angular 2', 'TypeScript', 'Visual Studio 2015'];
myskills = this.skills[1];
}
は、私はより多くの情報が必要な場合の何かを逃した場合、私に教えてくださいboot.ts。 index.cshtmlコード
UPDATEここ2
です:
@{
ViewBag.Title = "Home Page";
}
<my-app>Loading...</my-app>
_Layout.cshtmlがある。ここUPDATE
は、エラーのスクリーンショットです共有ビューとinde x.cshtmlは角型アプリがある場所です。他のMVCビューでも_Layout.cshtmlにすべてのコードが含まれます。
ありがとうございます!
あなたは 'angular'または' mvc'を通してルーティングしていますか? – Aravind
MVC。私は角度ルーティングロジックを追加していません。それは非常に基本的なアプリです。 –
あなたは直面しているエラーは何ですか? – Aravind