解决了! Angular 9路由器链接和路由器出口问题

时间:2020-02-26 10:09:20

标签: angular router-outlet routerlink

我目前正在做一个练习的问题,由于路线原因我无法完成,我有一个子视图,无法使用routerLink不可点击!

  • 我有一个视图,用于使登录名重定向到具有子组件的home组件,并且其中的路由链接不起作用,保持像普通文本一样。

  • 当我尝试使用路由器插座时 编译会提示错误:

    exercise / home / home.component.html:15:5中的错误-错误NG8001:“路由器出口”不是已知元素:

    1. 如果“ router-outlet”是Angular组件,请验证它是否属于此模块。
    2. 如果“路由器出口”是Web组件,则将“ CUSTOM_ELEMENTS_SCHEMA”添加到该组件的“ @ NgModule.schemas”以禁止显示此消息。

    15 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ p

    exercise / home / home.component.ts:6:16 6 templateUrl:“ ./ home.component.html”, ~~~~~~~~~~~~~~~~~~~~~~~ 组件HomeComponent的模板中发生错误。

这种情况是这样的:

1- app.component.html有一个导航栏,可以重定向到其他页面,并且工作正常,我的问题始于锻炼路线 2-视图是:

运动 登录 家 视图1 view2

3-我在登录pagem上有一个重定向到home的输入,后者具有一个导航栏重定向到view1和view2,但是“ routerLink”和“ router-outlet”不起作用。 4-我的文件在上面 5- Angular版本9.0.3

有人可以帮助我理解我做错了什么吗?!

========================= 解决方案:在声明上添加MISSING COMPONENT HomeComponent

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { NotFoundComponent } from './not-found/not-found.component';
import { UrlParamsComponent } from './url-params/url-params.component';
import { FormsModule } from '@angular/forms';
import { UserComponent } from './child-route/user/user.component';
import { ExerciseComponent } from './exercise/exercise.component';
import { SimpleRouteComponent } from './simple-route/simple-route.component';
import { CodeDrivenComponent } from './code-driven/code-driven.component';
import { QueryParamsComponent } from './query-params/query-params.component';
import { ChildRouteComponent } from './child-route/child-route.component';
import { LoginComponent } from './exercise/login/login.component';

@NgModule({
   declarations: [
      AppComponent,
      NotFoundComponent,
      UrlParamsComponent,
      UserComponent,
      ExerciseComponent,
      SimpleRouteComponent,
      CodeDrivenComponent,
      QueryParamsComponent,
      ChildRouteComponent,
      LoginComponent,
   ],
   imports: [
      BrowserModule,
      AppRoutingModule,
      FormsModule
   ],
   providers: [],
   bootstrap: [
      AppComponent
   ]
})
export class AppModule { }

=========================== app.component.html

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" routerLink="">Angular Routing</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav"
    aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" routerLink="/" routerLinkActive="active">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" routerLink="simple-route" routerLinkActive="active">Simple-route</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" routerLink="code-driven" routerLinkActive="active">Code-Driven</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" routerLink="url-params" routerLinkActive="active">Url-Params</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" routerLink="query-params" routerLinkActive="active">Query-Params</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" routerLink="child-route" routerLinkActive="active">Child-Route</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" routerLink="exercise/login" routerLinkActive="active">Exercise</a>
      </li>
    </ul>
  </div>
</nav>

<div class="container">
  <router-outlet></router-outlet>
</div>

===================================== app-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { NotFoundComponent } from './not-found/not-found.component';
import { UrlParamsComponent } from './url-params/url-params.component';
import { UserComponent } from './child-route/user/user.component';
import { USER_CHILD_ROUTES } from './child-route/user/user.routes';
import { HomeComponent } from './exercise/home/home.component';
import { HOME_CHILD_ROUTES } from './exercise/home/home-routing';
import { SimpleRouteComponent } from './simple-route/simple-route.component';
import { CodeDrivenComponent } from './code-driven/code-driven.component';
import { FirstRouteComponent } from './simple-route/first-route/first-route.component';
import { SecondRouteComponent } from './simple-route/second-route/second-route.component';
import { FirstCodeDriverComponent } from './code-driven/first-code-driver/first-code-driver.component';
import { SecondCodeDriverComponent } from './code-driven/second-code-driver/second-code-driver.component';
import { UrlParamsFirstComponent } from './url-params/url-params-first/url-params-first.component';
import { UrlParamsSecondComponent } from './url-params/url-params-second/url-params-second.component';
import { QueryParamsComponent } from './query-params/query-params.component';
import { QueryParamsFirstComponent } from './query-params/query-params-first/query-params-first.component';
import { ChildRouteComponent } from './child-route/child-route.component';
import { ExerciseComponent } from './exercise/exercise.component';
import { LoginComponent } from './exercise/login/login.component';

const routes: Routes = [

  // simple link routes
  { path: "simple-route", component: SimpleRouteComponent },
  { path: "simple-route/first-route", component: FirstRouteComponent },
  { path: "simple-route/second-route", component: SecondRouteComponent },

  // code routes
  { path: "code-driven", component: CodeDrivenComponent },
  { path: "code-driven/first-code-driver", component: FirstCodeDriverComponent },
  { path: "code-driven/second-code-driver", component: SecondCodeDriverComponent },

  // Using URL PARAMS
  { path: "url-params", component: UrlParamsComponent },
  { path: "url-params/url-params-first/:message", component: UrlParamsFirstComponent },
  { path: "url-params/url-params-second/:message", component: UrlParamsSecondComponent },

  // Using Query PARAMS
  { path: "query-params", component: QueryParamsComponent },
  { path: "query-params/query-params-first/:message", component: QueryParamsFirstComponent },

  // Child routes
  { path: "child-route", component: ChildRouteComponent },
  { path: "child-route/user/:id", component: UserComponent, children: USER_CHILD_ROUTES },

  // Exercise
  { path: "exercise", component: ExerciseComponent },
  { path: "exercise/login", component: LoginComponent },
  { path: "exercise/home/:username", component: HomeComponent, children: HOME_CHILD_ROUTES },

  // redirects to the home-root component
  // { path: "", redirectTo: "", pathMatch: "full" },

  // not found any route
  { path: "**", component: NotFoundComponent }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

这是我的问题,我无法点击路由器

================ home.component.ts

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {

  user: User = new User();

  constructor(activatedRoute: ActivatedRoute) {

    activatedRoute.params.subscribe((params) => {
      this.user.username = params['username'];
    });
  }

  ngOnInit() {
  }

}

class User {
  username: string;
}

================================= home.component.html

<hr>

<ul class="nav navbar-nav item">
    <li>
        <a routerLink='View1'>View1</a>
    </li>
    <li>
        <a routerLink="View2">View2</a>
    </li>
</ul>

<h1>Welcome Home, {{user.username}}</h1>

<div class="container">
    <!-- <router-outlet></router-outlet> -->
</div>
<br>
<a routerLink="exeercise/login">Logout</a>

===================================
home-routing.ts

import { Routes } from '@angular/router';
import { View1Component } from './view1/view1.component';
import { View2Component } from './view2/view2.component';

export const HOME_CHILD_ROUTES: Routes = [
    { path: "view1", component: View1Component },
    { path: "view2", component: View2Component }
];

====================================
login.component.ts

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {

  username: string;

  constructor(private router: Router) { }

  ngOnInit(): void {
  }

  login() {
    console.log('TRIGGER LOGIN...', this.username);
    this.router.navigate(['exercise/home', this.username]);
  }

}

============================ login.component.html

<hr>

<input [(ngModel)]="username" type="text">

<button (click)="login()">Login</button>

1 个答案:

答案 0 :(得分:0)

RouterModule导入到您的app.module.ts

import { RouterModule } from '@angular/router';

RouterModule添加到您的app.module.ts imports []

imports: [RouterModule]