gpt4 book ai didi

c# - 找不到 Angular 2 路由资源

转载 作者:太空狗 更新时间:2023-10-29 19:36:22 26 4
gpt4 key购买 nike

我正在使用 VS 2015 RC,在 WebAPI 项目上工作,当我尝试在 Angular 2 中使用路由时,出现以下错误:

Failed to load resource: the server responded with a status of 404 (Not Found) localhost:14580/angular2/router

Potentially unhandled rejection [3] Error loading "angular2/router" at localhost:14580/angular2/routerError loading "angular2/router" from "Components/main/main" at localhost:14580/Components/main/main.jsNot Found: localhost:14580/angular2/router (WARNING: non-Error used)

View 是 main.ts 组件的基本导入。组件代码如下:

/// <reference path="../../Scripts/typings/angular2/angular2.d.ts" />
/// <reference path="../../Scripts/typings/_custom/ng2.d.ts" />

import {Router, RouteConfig, RouterLink, RouterOutlet} from 'angular2/router';
import {Component, View, bootstrap} from 'angular2/angular2';
import {Login} from '../login/login';
import {status, json} from 'Scripts/utils/fetch'


// Annotation section
@Component({
selector: 'my-app'
})
@View({
templateUrl: 'Views/main/main.html',
directives: [RouterLink, RouterOutlet]
})
@RouteConfig([
{ path: '/login', as: 'login', component: Login }
])
// Component controller
export class Main {
//router: Router;
name: string;
constructor(router: Router) {
//this.router = router;
this.name = 'Routing';

router.config([{ path: '/login', as: 'login', component: Login }]);
}

login(event, email, password) {
event.preventDefault();

window.fetch('/api/Account/Login', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
email, password
})
})
.then(status)
.then(json)
.then((response) => {
alert(response);
// this.router.parent.navigate('/home');
})
.catch((error) => {
alert(error.message);
console.log(error.message);
});
}

}
bootstrap(
Main
);

最佳答案

您需要像包含 angular2.dev.js 一样在您的 html 中包含 router.dev.js。所以只需将这一行添加到您的 index.html 的头部:

<script src="../node_modules/angular2/bundles/router.dev.js"></script>

附言。我知道你已经解决了这个问题,但答案并不明确,我花了一段时间才意识到我的应用程序出了什么问题。

关于c# - 找不到 Angular 2 路由资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31400396/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com