简体   繁体   English

Aurelia/TS Activate 不更新当前视图

[英]Aurelia/TS Activate does not update current view

What do I want to achieve我想达到什么

I want to update my current view based on an Id.我想根据 Id 更新我当前的视图。 So say that I have side navigation with the following tabs:所以说我有带有以下标签的侧面导航:

  1. Customer A客户 A
  2. Customer B客户 B
  3. Customer C客户 C

What I want is that the user can click on Customer A and that the current customer view gets updated based on the Customer Id.我想要的是用户可以单击客户 A,并且当前客户视图会根据客户 ID 进行更新。

What is my problem achieving this我的问题是什么

I thought the best way to solve this issue was to navigate to the page and provide the Id directly as follows:我认为解决此问题的最佳方法是导航到页面并直接提供 Id,如下所示:

router.navigateToRoute("customer", { currentCustomerId });

Then on the Customer page I am receiving the Id in the activate method as following:然后在客户页面上,我收到激活方法中的 ID,如下所示:

  public activate(params) {
    this.currrentCustomerId = params.currentCustomerId;
  }

Actually, this is working the first time you navigate to a customer.实际上,这在您第一次导航到客户时起作用。 But when I am clicking on another Customer page, the view does not get updated because the activate method does not get triggered for a second time.但是当我单击另一个客户页面时,视图不会更新,因为激活方法不会被第二次触发。 It is only working if I navigate to another page (not customer page) and go back or simply refresh the whole page.只有当我导航到另一个页面(不是客户页面)并返回 go 或只是刷新整个页面时,它才有效。

So what can I use to achieve what I want?那么我可以用什么来实现我想要的呢? I reckon that I have to use something else than activate()?我认为我必须使用除激活()之外的其他东西?

I appreciate it if someone could give me some insight into this issue.如果有人能给我一些关于这个问题的见解,我将不胜感激。

Regards.问候。

This is due to the default activation strategy wherein, if the URL only changes in terms of a parameter value, the component is reused and hooks are not invoked.这是由于默认激活策略,如果 URL 仅在参数值方面发生变化,则重用组件并且不调用挂钩。

To obtain the desired behavior, you can customize the this behavior at the component level or the route level.要获得所需的行为,您可以在组件级别或路由级别自定义此行为。

At the component level:在组件级别:

import {activationStrategy} from 'aurelia-router';

export class CustomerComponent {
  determineActivationStrategy() {
    return activationStrategy.replace;
  }

  activate(params: {currrentCustomerId: string}) {
    this.currentCustomerId = params.currentCustomerId;
  }
}

At the route level:在路由级别:

import {Router, RouterConfiguration} from 'aurelia-router'; 

export class App {
   configureRouter(config: RouterConfiguration, router: Router) {
     config.map([{
       name: 'customer',
       moduleId: './customer',
       route: 'customer/:currentCustomerId',
       activationStrategy: 'replace'
     }]);

     this.router = router;
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM