简体   繁体   English

Angular2 - 无法获取/

[英]Angular2 - Cannot GET /

I'm trying to add routes to my Angular2 application (I'm using Angular2 rc4). 我正在尝试将路由添加到我的Angular2应用程序(我正在使用Angular2 rc4)。 However I'm getting a problem loading in new page. 但是我在新页面加载时出现问题。 In my app.comonent I can load all the routes fine with routerLink , but if I naviagte to the route via url it gives me Cannot GET / route. 在我的app.comonent中,我可以使用routerLink加载所有路由,但如果我通过url naviagte到路由,它会让我无法获取/路由。 It also loads a home.component without errors as well. 它还加载一个home.component也没有错误。 I tried adding HTTP_PROVIDERS, but no luck there. 我尝试添加HTTP_PROVIDERS,但没有运气。 Any idea what is causing this issue ? 知道是什么导致了这个问题吗?

boot.ts boot.ts

import {bootstrap} from '@angular/platform-browser-dynamic'
import {HTTP_PROVIDERS} from '@angular/http';
import {provideRouter} from '@angular/router';
// Componets
import {AppComponent} from './app/app.component'
import {APP_ROUTER_PROVIDERS} from './app/app.routes';

bootstrap(AppComponent, [
    provideRouter(APP_ROUTER_PROVIDERS),
    HTTP_PROVIDERS,
    APP_ROUTER_PROVIDERS
]).catch(err => console.error(err));

app.routes.ts app.routes.ts

import {provideRouter, RouterConfig} from '@angular/router';
// Components 
import {HomeComponent} from './home/home.component';
import {StoreComponent} from './store/store.component';


export const routes: RouterConfig = [

    { path: '', component: HomeComponent },
    { path: 'store', component: StoreComponent },

];

export const APP_ROUTER_PROVIDERS = [
    provideRouter(routes)
];

home.component.ts home.component.ts

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

//const template = require('./home.component.html'); 
const template = 'src/app/home/home.component.html'; 

@Component({
  selector: 'my-home',
  templateUrl: template
})

export class HomeComponent {

}

store.component.ts store.component.ts

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

const template = 'src/app/store/store.component.html'; 
const styles = '/store.component.css'; 

// Mock Models 
interface Product {
    id: number;
    logo: string;
    name: string;
    price: number;
}; 
let Categories:string[] = ['Convertible','Sports Car','Truck','Suv'] ;
let Products:Product[] = [
    { id: 0, logo: '/images/Logo.png', name: "Food ", price: 1000 },
    { id: 1, logo: '/images/Logo.png', name: "Shelter", price:2000 },
    { id: 2, logo: '/images/Logo.png', name: "Health", price:3000 },
    { id: 3, logo: '/images/Logo.png', name: "Advocacy", price: 4000},
    { id: 4, logo: '/images/Logo.png', name: "Funding", price: 5000 },
];

@Component({
    selector: 'my-store',
    templateUrl : template
})

export class StoreComponent {
    categories:string[] = Categories ; 
    products:Product[] = Products ; 

}

Remove this line 删除此行

APP_ROUTER_PROVIDERS

from bootstrap(...) . 来自bootstrap(...) This is already provided by provideRouter(...) 这已由provideRouter(...)

I think pathMatch: 'full' is required for this route 我认为pathMatch: 'full'此路线需要pathMatch: 'full'

    { path: '', component: HomeComponent, pathMatch: 'full' },

but it seems to also work without for others. 但它似乎也适用于其他人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM