Навигация на домашней странице не работает в угловых 2 - PullRequest
0 голосов
/ 04 декабря 2018

Я перешел со страницы входа на домашнюю страницу. На главной странице, если я нажимаю на вопросы, новые вопросы или ссылки, соответствующие страницы не отображаются на домашней странице (http://localhost/home).

Вот мойвсе фрагменты кода

app.module.ts

var myroutes:Routes=[
{path:"",redirectTo:"home",pathMatch:"full"},
{path:"home",component:HomeComponent},
{path: 'userdetails',component:UserdetailsComponent},
{path:'questions',component:QuestionsIndexComponent},
{path:'questionsnew',component:QuestionsNewComponent  },
{path:'questionsview',component:QuestionsViewComponent},
{path:'FancyButtons',component:FancybuttonsComponent}];
var allroutes=RouterModule.forRoot(myroutes);

index.html

<body>
  <app-root></app-root>
</body>

app.component.html

<div id="appdesign" align="center" *ngIf="showButton">
  <br>
  <br><br><br><br><br><br>
  <h1>MY FIRST PROJECT IN ANGULAR2</h1>
  <br><br><br><br><br><br><br><br>

 <h4>Existing Users can: </h4>
  <button style="height: 25px;width:50px" routerLink="/login" (click)="btnloginclick()">Login</button>
  <br><br><br><br><br>
  <h4>New User can:</h4>     
<button style="height: 25px;width:50px" routerLink="/register">Register</button>
</div>    
 <router-outlet></router-outlet>

логин.component.html

<div id="logindiv">
  <h4>Login</h4>
<form #myloginform="ngForm">
Email : <input type="text"[(ngModel)]="email" required="required" #ctrlemail="ngModel"  name="email" ><br>
<span class="error" *ngIf="ctrlemail.invalid && ctrlemail.touched && ctrlemail.errors.required">EMAIL CANT BE BLANK</span>
Password: <input type="password" [(ngModel)]="password" name="password"  ><br>
<input type="submit" value="Login" (click)="onLoginClick(myloginform)"><br>
  {‌{message}}
</form>
</div>

login.component.ts

onLoginClick(myloginform)
{
if (myloginform.valid==true)
{
    this.objroute.navigate(['home']);
}

Home.component.html

<div id="header">
  <br>
<a routerLink="userdetails" routerLinkActive="active">UserDetails</a>&nbsp;    
  <a routerLink="questions">Questions</a>&nbsp;
  <a routerLink="questionsnew">New Question</a>&nbsp;
  <a routerLink="questionsview">View Question</a>&nbsp;
  <a routerLink="FancyButtons">Fancy Buttons</a>
  <br>
</div>

<div id="pagecontent">
  <router-outlet></router-outlet>
</div>

<div id="footer">
</div>

Укажите мою проблему и предоставьте мне правильное решение, чтобы я мог продолжить мой пример проекта.

Спасибо

Раджула

Ответы [ 2 ]

0 голосов
/ 04 декабря 2018

это потому, что URL, по которому вы пытаетесь перейти, не существует, попробуйте это так

<a routerLink="../questions">Questions</a>&nbsp;
  <a routerLink="../questionsnew">New Question</a>&nbsp;
  <a routerLink="../questionsview">View Question</a>&nbsp;
  <a routerLink="../FancyButtons">Fancy Buttons</a>
0 голосов
/ 04 декабря 2018

Попробуйте установить страницы внутри нового компонента, LayoutComponent как дочерние:

{path:"",component: LayoutComponent, children: [
 {path: '', pathMatch: 'full', redirectTo: '/home'},
 {path:"home",component:HomeComponent}
 {path: 'userdetails',component:UserdetailsComponent},
 {path:'questions',component:QuestionsIndexComponent},
 {path:'questionsnew',component:QuestionsNewComponent  },
 {path:'questionsview',component:QuestionsViewComponent},
 {path:'FancyButtons',component:FancybuttonsComponent}
]}

Итак, создайте компонент Layout, поместите в него маршрутизатор-розетку и выполните маршрутизацию следующим образом.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...