在Angular中使用Resolve重新加载子路线的问题

时间:2019-02-21 22:37:28

标签: angular angular-resolver

我在重新加载childComponent函数时遇到问题。我有一个组件,可以根据接收到的参数对数据库进行查询。 当我第一次访问该路由时,它会很好地执行OnInit函数,但是如果我更改参数:page,它什么也不会做。

以下是组件:

//temas.children.component.ts
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';

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


  constructor(private route: ActivatedRoute) { }

  ngOnInit() {
     console.log(this.route.snapshot.data.temas);

  }
}

路线:

//app.routes.ts
//imports and some other routes
{ path: 'temas/:categoria', component: TemasComponent, children:[
{path: ':page', component: TemasChildrenComponent, runGuardsAndResolvers: "always", resolve:{temas: TemasResolverService}}
]},

解析器服务:

//temas.resolver.service.ts
import { Injectable } from '@angular/core';
import { CrudService } from './crud.service';
import { Resolve } from '@angular/router';
import { ActivatedRouteSnapshot } from '@angular/router';

@Injectable({
  providedIn: 'root'
})
export class TemasResolverService implements Resolve<any>{

constructor(private crudService: CrudService) { }
  resolve(route:ActivatedRouteSnapshot){
    return this.crudService.getTemas(route.parent.pathFromRoot[1].params.categoria, route.paramMap.get('page'));
  }
}

0 个答案:

没有答案