如何在不破坏组件angular5的情况下更改url

时间:2018-09-25 09:13:46

标签: angular typescript

对于Angular2世界来说只是一个新手,但是通过指定{reload:false},它在angular 1中非常简单,但是无法在Angular 2中找到类似的解决方案,在该解决方案中,我可以避免在更改查询参数时重新加载组件我的路线。

我有html模板,单击按钮即可更改查询参数。

我有方法updateRoute({type:'app_dependency'}),只需单击按钮即可调用:

@Transactional
public interface Rank extends CrudRepository<RankArticle, Identity> {

    @Modifying
    @Query(value = "INSERT INTO rank" +
                        "(SELECT id, shop_id, SUM(sales) AS score, CAST(NULL AS INT) AS rank " +
                        "FROM " +
                            "(SELECT id, shop_id, sales FROM daily_pod " +
                            "WHERE shop_id = :shopId " +
                            "UNION " +
                            "SELECT id, shop_id, sales FROM weekly_pod " +
                            "WHERE shop_id = :shopId " +
                            "UNION " +
                            "SELECT id, shop_id, sales FROM end_pod " +
                            "WHERE shop_id = :shopId) " +
                        "AS pods " +
                        "GROUP BY id, shop_id ORDER BY score DESC LIMIT :limit) " +
                "ON CONFLICT (id) DO UPDATE " +
                "SET shop_id = EXCLUDED.shop_id, score = EXCLUDED.score, " +
                    "rank_count = EXCLUDED.rank_count;"
        , nativeQuery = true)
void calcScore(@Param("shopId") Integer shopId,
               @Param("limit") Integer limit);

但是,每次调用此函数时,它都会破坏该组件并调用ngOnInit()。

是否可以仅更改url查询(我不更改路线,而是更改查询)而无需重新加载组件?如果是,请问有人可以帮我还是建议我一种更好的方法。

1 个答案:

答案 0 :(得分:0)

我在这里找到了解决方法:
这样可以防止组件被破坏。

constructor(
        private router: Router
      ) {
        this.router.routeReuseStrategy.shouldReuseRoute = function () {
          return true;
        };
      }