角路由器布局部分起作用

时间:2019-10-26 18:03:05

标签: angular

app.component.ts:

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

@Component({
  selector: 'app-root',
  template: "<router-outlet></router-outlet>"
  //templateUrl: './app.component.html',
  //styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'AngularTest';
  getData() {
    this.title = 'hello';
  }
}

app.module

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { RouterModule } from "@angular/router";


@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    RouterModule.forRoot([
      {
        path: "site",
        loadChildren: "./site/site.module#SiteModule"
      },
      {
        path: "admin",
        loadChildren: "./admin/admin.module#AdminModule"
      }
      //{ path: "**", redirectTo: "/site/main/checkout" }
  ])],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

/site/site.module:

import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { FormsModule } from "@angular/forms";
import { ModelModule } from "../model/model.module";
import { SiteComponent } from "./site.component";
import { CheckoutComponent } from "./siteComponents/checkout.component";
import { RouterModule } from "@angular/router";
import { CommonModule } from "@angular/common";

let routing = RouterModule.forChild([    
    { path: "main", component: SiteComponent,
        children: [
            { path: "checkout", component: CheckoutComponent }
            //{ path: "**", redirectTo: "checkout" }
        ]
    }
    //{ path: "**", redirectTo: "main" }
]);

@NgModule({
    imports: [ModelModule, BrowserModule, FormsModule, CommonModule, routing],
    declarations: [SiteComponent, CheckoutComponent],                        //CounterDirective, CartSummaryComponent,CartDetailComponent, 
    exports: [RouterModule]  //CartDetailComponent
})
export class SiteModule { }

/site/site.component:

    <div class="container-fluid">
  <div class="row">
    <div class="col bg-dark text-white">
      <a class="navbar-brand">SPORTS STORE</a>
      <!--<cart-summary></cart-summary>-->
    </div>
  </div>
  <div class="row">
  </div>
  <div class="row">
    <div class="col-12">
      <router-outlet></router-outlet>
  </div>
  </div>
</div>

和其他文件/site/siteComponents/checkout.component.ts和html。

当我输入http://localhost:3500/site/main/checkout时,适用于admin的布局有效,但适用于site.component,浏览器将重定向至作为根页面的http://localhost:3500/。 要更改site.component布局,需要进行哪些更改?

0 个答案:

没有答案