Angular的路由配置方法有哪些?
- 内容介绍
- 文章标签
- 相关推荐
本文共计349个文字,预计阅读时间需要2分钟。
以下是对所提供内容的简化
plaintext一些实用的Angular代码片段:import { Component, OnInit } from '@angular/core';import { HeroService } from '../hero.service';import { Router, ActivatedRoute, ParamMap } from '@angular/router';import { Hero } from '../hero';
路由的一些简单实用代码import { Component, OnInit } from '@angular/core'; import {HeroService} from "../hero.service"; import {Router, ActivatedRoute, ParamMap} from "@angular/router"; import {Hero} from "../hero"; import {Location} from "@angular/common"; import 'rxjs/add/operator/switchMap'; @Component({ selector: 'app-hero-detail', templateUrl: './hero-detail.component.html', styleUrls: ['./hero-detail.component.css'] }) export class HeroDetailComponent implements OnInit { hero:Hero; constructor(private hreoService:HeroService,private route:ActivatedRoute,private location:Location) { } ngOnInit():void{ this.route.paramMap.switchMap((parms:ParamMap)=> this.hreoService.getHero(+parms.get("id")) ).subscribe(hero=>this.hero=hero); } goBack():void{ this.location.back(); } }
hero-detail works!
{{hero.name}} details!
{{hero.id}} 异步取得数据: import { Injectable } from '@angular/core'; import {Hero} from "./hero"; import {HEROES} from "./mock-heroes"; @Injectable() export class HeroService { constructor() { } getHeroes(): Promiseimport { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; const routes: Routes = []; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule { } 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'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, AppRoutingModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
本文共计349个文字,预计阅读时间需要2分钟。
以下是对所提供内容的简化
plaintext一些实用的Angular代码片段:import { Component, OnInit } from '@angular/core';import { HeroService } from '../hero.service';import { Router, ActivatedRoute, ParamMap } from '@angular/router';import { Hero } from '../hero';
路由的一些简单实用代码import { Component, OnInit } from '@angular/core'; import {HeroService} from "../hero.service"; import {Router, ActivatedRoute, ParamMap} from "@angular/router"; import {Hero} from "../hero"; import {Location} from "@angular/common"; import 'rxjs/add/operator/switchMap'; @Component({ selector: 'app-hero-detail', templateUrl: './hero-detail.component.html', styleUrls: ['./hero-detail.component.css'] }) export class HeroDetailComponent implements OnInit { hero:Hero; constructor(private hreoService:HeroService,private route:ActivatedRoute,private location:Location) { } ngOnInit():void{ this.route.paramMap.switchMap((parms:ParamMap)=> this.hreoService.getHero(+parms.get("id")) ).subscribe(hero=>this.hero=hero); } goBack():void{ this.location.back(); } }
hero-detail works!
{{hero.name}} details!
{{hero.id}} 异步取得数据: import { Injectable } from '@angular/core'; import {Hero} from "./hero"; import {HEROES} from "./mock-heroes"; @Injectable() export class HeroService { constructor() { } getHeroes(): Promiseimport { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; const routes: Routes = []; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule { } 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'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, AppRoutingModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }

