错误TypeError:“ _ co.navigate不是函数”的角度5

时间:2019-12-06 09:52:50

标签: javascript angular5 angular8

        this is register.component.html 
        <div class="row">
           <div class="col s6 offset-s3">
              <div class="card">
                <div class="col s6">
                  <h2>Quiz Registration</h2>
                </div>
                <div class="col s6">
                     <img src="/assets/img/hello.jpg" style="width:250px;height:250px;" class="offset-s3">
                </div>
                 <div class="card-content">
                   <form #registerForm="ngForm" (ngSubmit)="OnSubmit(Name.value,Email.value)">
                        <div class="row">
                          <div class="input-field col s12">
                            <i class="material-icons prefix">account_circle</i>
                           <input type="text" name="Name" #Name ngModel required>
                           <label>Name</label>
                          </div>
                        </div>
                        <div class="row">
                            <div class="input-field col s12">
                              <i class="material-icons prefix">mail_outline</i>
                             <input type="text" name="Email" #Email ngModel required [pattern]="emailPattern">
                             <label>Email</label>
                            </div>
                          </div>
                          <div class="row">
                           <div class="input-field col s12">
                               <button  class="btn-large btn-submit"  type="submit"  [disabled]="!registerForm.valid">Start</button>

                           </div>

                          </div>


                   </form>

                  </div>
                </div>
        </div>
        </div>
    This is register.component.ts
    import { Component, OnInit } from '@angular/core';
    import { QuizService } from '../shared/quiz.service';
    import {Router} from '@angular/router';
    @Component({
      selector: 'app-register',
      templateUrl: './register.component.html',
      styleUrls: ['./register.component.css']
    })
    export class RegisterComponent implements OnInit {
      emailPattern = "^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$";
      constructor(private quizService : QuizService,private route : Router) { }

      ngOnInit() {
      }
    OnSubmit(name:string,email:string)
    {
    this.quizService.insertParticipant(name,email).subscribe(
       (data : any) => {
         localStorage.clear();
          localStorage.setItem('participant',JSON.stringify(data));
         this.route.navigate(['/quiz']);
     });
    }

    }
i m trying to submit a form where name and email is given and when i click on submit button ,the page should redirect from http://localhost:4200/register to http://localhost:4200/quiz but the page is on the same page(http://localhost:4200/register) and is not redirecting and i m getting error in console Http failure response for (unknown url): 0 Unknown Error and why it is not redirecting to another page

代码中的问题是什么? 如果需要更多代码,我会帮助您 我正在使用angular5,我认为问题出在这一行this.route.navigate(['/ quiz']); 代码中有什么问题? 如果需要更多代码,我会帮助您 我正在使用angular5,我认为问题出在这一行this.route.navigate(['/ quiz']);

1 个答案:

答案 0 :(得分:0)

您尝试这样吗?

this.route.navigate(["quiz"]);