0

i am doing basic student application, where i have a problem understanding routing.

app.modules.ts:

import { NgModule }       from '@angular/core';
import { BrowserModule }  from '@angular/platform-browser';
import { FormsModule }    from '@angular/forms';
//import { HttpModule, JsonpModule, XHRBackend } from '@angular/http';

//import { InMemoryBackendService } from 'angular2-in-memory-web-api';
import { AppComponent }   from './app.component';
import { routes }        from './app.routes';

import { StudentsComponent }      from './students.component';
//import { AddStudentComponent }   from './add-student.component';
//import { StudentDetailComponent }  from './student-detail.component';

import { StudentService }  from './student.service';

@NgModule({
  imports: [
BrowserModule,
FormsModule,
routes,
//    HttpModule,
//    JsonpModule
],
declarations: [
AppComponent,
StudentsComponent,
// AddStudentComponent,
//    StudentDetailComponent
],
providers: [
 StudentService,
 //    { provide: XHRBackend, useClass: InMemoryBackendService }, 
],
bootstrap: [ AppComponent ]
})
export class AppModule {
}

app.routes.ts:

import { Routes, RouterModule } from '@angular/router';
import { ModuleWithProviders }  from '@angular/core';
//import { AddStudentComponent } from './add-student.component';

import { StudentsComponent } from './students.component';
//import { StudentDetailComponent } from './student-detail.component';

const appRoutes: Routes = [
{
    path: '',
    redirectTo: '/students',
    pathMatch: 'full'
},
{
    path: 'students',
    component: StudentsComponent
}
//  {
//    path: 'add',
//    component: AddStudentComponent
//  },
//  {
//    path: 'update/:roll',
//    component: StudentDetailComponent
//  },

];

export const routes: ModuleWithProviders = RouterModule.forRoot(appRoutes);

I don't understand what's wrong in routing, it isn't working.. I am trying to display students component as default page.

4
  • What does "it isn't working" mean? Commented Aug 29, 2016 at 13:04
  • What server are you using? Do you have the <base href="/"> as first child in your <head> tag? Commented Aug 29, 2016 at 13:04
  • <script>document.write('<base href="' + document.location + '" />');</script> this line is given after head tag. Commented Aug 29, 2016 at 13:07
  • i have given route /students for StudentsComponent, when i running that route, its showing url not found on server. By the way i am using apache2 Commented Aug 29, 2016 at 13:10

2 Answers 2

1

Problem is not with routing. There are 2 things i have been modified.

1) In app.modules.ts the below line should be added:

import { HttpModule, JsonpModule } from '@angular/http';

and

2)In imports of app.modules.ts the comments i kept before HttpModule and JsonpModule have to removed. i.e add

HttpModule,
JsonpModule

in imports.

Thank you all, Who supported.

Sign up to request clarification or add additional context in comments.

Comments

0

Apache tries to serve /students which obviously isn't present on the server. Apache can't know about angular's routes.

There are (at least) two ways to approach this:

Server-side:
Rewrite any (non-file) requests to index.html as described here

Client-side:
Switch to HashLocationStrategy

@NgModule({
  providers: [{provide: LocationStrategy, useClass: HashLocationStrategy}]
})
class AppModule {}

1 Comment

Thanks. But i want to use PathLocationStrategy. Can you help me for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.