angular resolve路由

import { Resolve, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from "@angular/router";
import { Product } from "../product/product.component";
import { Observable } from "rxjs/Observable";
import { Injectable } from "@angular/core";

@Injectable()
export class ProductResolve implements Resolve<Product> {
    constructor(private router: Router) { }
    resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Product | Observable<Product> | Promise<Product> {
        let productId: number = route.params["id"];
        if (productId == 1) {
            return new Product("pingguo7", 1);
        }
        else {
            this.router.navigate([‘/home‘]);
            return undefined;
        }
    }
}
import { NgModule } from ‘@angular/core‘;
import { Routes, RouterModule } from ‘@angular/router‘;
import { HomeComponent } from ‘./home/home.component‘;
import { ProductComponent } from ‘./product/product.component‘;
import { Code404Component } from ‘./code404/code404.component‘;
import { ProductDescComponent } from ‘./product-desc/product-desc.component‘;
import { SellerInfoComponent } from ‘./seller-info/seller-info.component‘;
import { ChatComponent } from ‘./chat/chat.component‘;
import { LoginGuard } from ‘./guard/login.guard‘;
import { UnsacedGuard } from ‘./guard/unsaced.guard‘;
import { ProductResolve } from ‘./guard/product.resolve‘;

const routes: Routes = [
  {
    path: ‘‘,
    redirectTo: ‘/home‘,
    pathMatch: ‘full‘
  },
  {
    path: ‘chat‘,
    component: ChatComponent,
    outlet: ‘aux‘
  },
  {
    path: ‘home‘,
    component: HomeComponent
  },
  {
    path: ‘product/:id‘,
    component: ProductComponent,
    children: [
      { path: ‘‘, component: ProductDescComponent },
      { path: ‘seller/:id‘, component: SellerInfoComponent }
    ],
    resolve: {
      product: ProductResolve
    }
  }
  ,
  {
    path: ‘**‘,
    component: Code404Component
  }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule],
  providers: [LoginGuard, UnsacedGuard, ProductResolve]
})
export class AppRoutingModule { }
import { Component, OnInit } from ‘@angular/core‘;
import { ActivatedRoute, Params } from ‘@angular/router‘;

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

  private productId: number;
  private productName: string;
  constructor(private routeInfo: ActivatedRoute) { }

  ngOnInit() {
    this.routeInfo.params.subscribe((params: Params) => this.productId = params["id"]);
    this.routeInfo.data.subscribe((data: { product: Product }) => {
      this.productId = data.product.id;
      this.productName = data.product.Name;
    });
    //this.productId = this.routeInfo.snapshot.params["id"];
  }

}

export class Product {
  constructor(public Name: string, public id: number) { }
}

原文地址:https://www.cnblogs.com/chenyishi/p/8904172.html

时间: 2024-10-31 16:47:21

angular resolve路由的相关文章

angular 4 路由风格 #锚点风格

当angular 编译完成后.部署到服务器,点击标签跳转路由.此时刷新页面.页面跳转403,我改回了锚点路径可以跳转页面后,刷新浏览器依旧显示正确页面.应该有其他方式让浏览器刷新后依旧正常工作的h5路由器. 改成锚点路径的代码如下 import {LocationStrategy, HashLocationStrategy} from '@angular/common'; { provide: LocationStrategy, useClass: HashLocationStrategy },

Angular 添加路由

var app=angular.module('kaifanla',['ng','ngRoute']);app.config(function($routeProvider){ //添加路由 $routeProvider .when('/文件名',{ templateUrl:'tpl/start.html', controller:'startCtrl' }) .when('/文件名',{ templateUrl:'tpl/main.html', controller:'mainCtrl' })

angular的路由跳转,的监听$rootScope.$on

使用angular来做项目时,习惯性的使用第三方路由插件ui-router配置路由.每一个状态都对应着一个页面, 因此对路由状态改变的监听也变的十分重要. 可以使用:$rootScope.$on(--.)监听 $stateChangeStart: 表示状态切换开始 $stateNoFound:没有发现 $stateChangeSuccess:切换成功 $stateChangeError:切换失败 回调函数的参数解释: event:当前事件的信息 toState:目的路由状态 toParams:传

angular 通过路由获取ID

constructor( private dataService: TestListsService, private route: ActivatedRoute, ) { // 通过路由获取ID console.log(route); this.id = this.route.params.value['id']; } import { ActivatedRoute } from '@angular/router'; 原文地址:https://www.cnblogs.com/linsx/p/8

angular 2+ 路由守卫

1. 定义接口名称 /domain/login-guard.ts export interface LoginGuard { data: any; msg: string; status: boolean; } 2. 定义actions  /ngrx/actions/login-guard.action.ts import { Action } from '@ngrx/store'; import {LoginGuard} from '../../domain/login-guard'; /**

Angular创建路由从主界面跳转到我们的cesium界面

我们要在一个独立的顶级模块中加载和配置路由器,它专注于路由功能,然后由根模块 AppModule 导入它. 按照惯例,这个模块类的名字叫做 APPRoutingModule,并且位于 src/app 下的 app-routing.module.ts 文件中. 使用 CLI 生成它. 1.在命令窗口输入ng generate module app-routing --flat --module=app --flat 把这个文件放进了 src/app 中,而不是单独的目录中.--module=app

angular之路由

一.核心问题 路由要解决的核心问题是通过建立url和页面之间的对应的关系,使不同的页面可以通过不用的url来展示. 首先,当用户在浏览器上输入URL后,Angular将获取该URL并将其解析生成一个UrlTree实例 其次,在路由配置中寻找并激活与UrlTree实例匹配的配置项 再次,为配置项中指定的组件创建实例 最后,将该组件渲染于路由组件的模板中<router-outlet>指令所在的位置 二.安装或者下载angular-route的包 通过npm或者cnpm安装,npm/cnpm ins

6、Angular Route 路由

1.没有嵌套路由 类似 ui-route 上述的html用红字标记的是必须导入的.这是因为:路由并不在ng2中,需要我们额外引入,另外我们需要设置base href,这是个什么东西呢?相当于我们后续所有url的“前缀”,因为我们的app默认是基于"HTML 5 pushState" 风格的路由,所以我们需要加上base href,来保证当我们导航到深层次的url时候,资源可以被正确加载. 2.带参数 和 嵌套的 route

Angular 4 路由

1. 创建工程 ng new router --routing 2. 创建home和product组件 ng g component home ng g component product 3. 配置路由 4. 主页面导航