第三张resolve守卫里面,为何照视频敲得代码,点击商品,没有显示出来呢

来源:3-8 resolve守卫

慕粉1629055924

2017-05-24

写回答

3回答

最外的红

2017-06-19

我把这一句删了就能用了(虽然时间久远。但是还是回一下给后人用吧)

    this.routeInfo.params.subscribe((params: Params) => this.productId = params['id']);

这个应该是重复了ID的获取。

1
0

慕粉1629055924

提问者

2017-05-26

/***
app-routing.module.ts
***/
const routes: Routes = [
  {path: '', redirectTo: '/home', pathMatch: 'full'},
  {path: 'chat', component: ChatComponent, outlet: 'aux'},
  {path: 'home', component: HomeComponent},
  {
    path: 'product/:id', component: ProductComponent,
    resolve: {
      product: ProductResolve
    },
    children: [
      {path: '', component: ProductDescComponent},
      {path: 'seller/:id', component: SellerInfoComponent}
    ]
  },

  {path: '**', component: Code404Component},
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule],
  providers: [ProductResolve]
})


/***
*product.component.html
**/
<div class="product">
<p> 这里是商品信息组件</p>
<p>
  商品的ID是:{{productId}}
</p>
  <p>商品名称是:{{productName}}</p>
<a [routerLink]="['./']">商品描述</a>
<a [routerLink]="['./seller',99]">销售员信息</a>
<router-outlet></router-outlet>
</div>

/**
**product.component.html
**/
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.productId = this.routeInfo.snapshot.queryParams['id'];
    // this.productId = this.routeInfo.snapshot.params['id'];
    /***
     * 参数订阅
     */
    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;
    });
  }
}

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

  }
}

/**
**app.component.html
**/
<a [routerLink]="['/home']">主页</a>
<!--<a [routerLink]="['/product']" [queryParams]="{id:1}">商品详情</a>-->
<a [routerLink]="['/product',1]" >商品详情</a>
<input type="button" value="商品详情" (click)="toProductDetail()">

<a [routerLink]="[{outlets:{primary:'home',aux:'chat'}}]">开始聊天 </a>
<a [routerLink]="[{outlets:{aux:null}}]">结束聊天</a>
<router-outlet></router-outlet>
<router-outlet name="aux"></router-outlet>

/**
**app.component.ts
**/
import {Component} from '@angular/core';

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app works!';

  constructor(private  router: Router) {

  }

  toProductDetail() {
    // this.router.navigate(['/product']);
    this.router.navigate(['/product', 1]);
  }
}


0
0

JoJo

2017-05-26

嗯,把代码贴上来看一下。

0
1
慕粉1629055924
请您看下,我把代码贴上来了。
2017-05-26
共1条回复

Angular4.0从入门到实战 打造股票管理网站

Angular新特性,教你熟练使用 Angular 和 Typescript 开发组件式单页应用

2683 学习 · 1361 问题

查看课程