Angular: How to block RouteReuseStrategy based on condition?

I’m trying to block the RouteReuseStrategy based on some conditions but this is not happening. I’ve a route name as comp-a which is called in two different ways. 1 time is from a back button and the other time from a button in header.

Case 1:
If there’s something happened in Component A and then user goes to Component B and there the user clicks on back button the data should persist in Component A. This case is achieved perfectly by using RouteReuseStrategy.

Case 2:
If there’s something happened in Component A and then user goes to Component B and there the user clicks on the button from header then when the user lands on Component A, the page should load from start i.e. ngOnInit() should get called.

My issue is for the Case 2 which is not working.

I tried to pass data in the route and access it from ActivatedRouteSnapshot in the shouldDetach() function but this is not helping. I took reference from here.

In app-routing.module.ts,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const route: Routes = [
{ path: 'comp-a', component: CompAComponent, data: { test: true } }
];
</code>
<code>const route: Routes = [ { path: 'comp-a', component: CompAComponent, data: { test: true } } ]; </code>
const route: Routes = [
  { path: 'comp-a', component: CompAComponent, data: { test: true } }
];

In app.component.ts

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// Click event on "Goto Comp A from Comp B" button
navigate() {
this.router.navigate(['../comp-a'], { state: { data: { test: false } } });
}
</code>
<code>// Click event on "Goto Comp A from Comp B" button navigate() { this.router.navigate(['../comp-a'], { state: { data: { test: false } } }); } </code>
// Click event on "Goto Comp A from Comp B" button
navigate() {
  this.router.navigate(['../comp-a'], { state: { data: { test: false } } });
}

In custom-reuse-strategy.ts,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>shouldDetach(route: ActivatedRouteSnapshot) {
return route.routeConfig?.path === 'comp-a' && (route.data as any)?.test; // I've accessed "test" variable here
}
</code>
<code>shouldDetach(route: ActivatedRouteSnapshot) { return route.routeConfig?.path === 'comp-a' && (route.data as any)?.test; // I've accessed "test" variable here } </code>
shouldDetach(route: ActivatedRouteSnapshot) {
  return route.routeConfig?.path === 'comp-a' && (route.data as any)?.test; // I've accessed "test" variable here
}

I’ve tried this in this stackblitz example.

In the above example, the button Goto Comp A from Comp B is for the Case 2 example and the button Go to Comp B is for Case 1.

How should I modify the route-reuse code or is there any other way to achieve this?

Yes, It should be achieved, but just with some modifications in the stackblitz example.

In app-routing.module.ts

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const routes: Routes = [
{ path: 'comp-a', component: CompAComponent, data: { test: true } },
{ path: 'comp-b', component: CompBComponent },
];
</code>
<code>const routes: Routes = [ { path: 'comp-a', component: CompAComponent, data: { test: true } }, { path: 'comp-b', component: CompBComponent }, ]; </code>
const routes: Routes = [
  { path: 'comp-a', component: CompAComponent, data: { test: true } },
  { path: 'comp-b', component: CompBComponent },
];

In app.component.ts

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>navigateFromHeader() {
this.router.navigate(['comp-a'], { state: { fromHeader: true } });
}
navigateFromBack() {
this.router.navigate(['comp-a'], { state: { fromHeader: false } });
}
</code>
<code>navigateFromHeader() { this.router.navigate(['comp-a'], { state: { fromHeader: true } }); } navigateFromBack() { this.router.navigate(['comp-a'], { state: { fromHeader: false } }); } </code>
navigateFromHeader() {
  this.router.navigate(['comp-a'], { state: { fromHeader: true } });
}

navigateFromBack() {
  this.router.navigate(['comp-a'], { state: { fromHeader: false } });
}

In custom-reuse-strategy.ts,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>shouldDetach(route: ActivatedRouteSnapshot): boolean {
return route.data?.test || false;
}
shouldAttach(route: ActivatedRouteSnapshot): boolean {
const fromHeader = history.state?.fromHeader;
if (fromHeader) {
this.storedRoutes[route.routeConfig?.path || ''] = null;
return false;
} else {
return !!this.storedRoutes[route.routeConfig?.path || ''];
}
}
</code>
<code>shouldDetach(route: ActivatedRouteSnapshot): boolean { return route.data?.test || false; } shouldAttach(route: ActivatedRouteSnapshot): boolean { const fromHeader = history.state?.fromHeader; if (fromHeader) { this.storedRoutes[route.routeConfig?.path || ''] = null; return false; } else { return !!this.storedRoutes[route.routeConfig?.path || '']; } } </code>
shouldDetach(route: ActivatedRouteSnapshot): boolean {
  return route.data?.test || false;
}

shouldAttach(route: ActivatedRouteSnapshot): boolean {
 const fromHeader = history.state?.fromHeader;
  if (fromHeader) { 
    this.storedRoutes[route.routeConfig?.path || ''] = null;
     return false;
  } else {
    return !!this.storedRoutes[route.routeConfig?.path || ''];
  }
}

New contributor

Vaibhav Gupta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Angular: How to block RouteReuseStrategy based on condition?

I’m trying to block the RouteReuseStrategy based on some conditions but this is not happening. I’ve a route name as comp-a which is called in two different ways. 1 time is from a back button and the other time from a button in header.

Case 1:
If there’s something happened in Component A and then user goes to Component B and there the user clicks on back button the data should persist in Component A. This case is achieved perfectly by using RouteReuseStrategy.

Case 2:
If there’s something happened in Component A and then user goes to Component B and there the user clicks on the button from header then when the user lands on Component A, the page should load from start i.e. ngOnInit() should get called.

My issue is for the Case 2 which is not working.

I tried to pass data in the route and access it from ActivatedRouteSnapshot in the shouldDetach() function but this is not helping. I took reference from here.

In app-routing.module.ts,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const route: Routes = [
{ path: 'comp-a', component: CompAComponent, data: { test: true } }
];
</code>
<code>const route: Routes = [ { path: 'comp-a', component: CompAComponent, data: { test: true } } ]; </code>
const route: Routes = [
  { path: 'comp-a', component: CompAComponent, data: { test: true } }
];

In app.component.ts

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// Click event on "Goto Comp A from Comp B" button
navigate() {
this.router.navigate(['../comp-a'], { state: { data: { test: false } } });
}
</code>
<code>// Click event on "Goto Comp A from Comp B" button navigate() { this.router.navigate(['../comp-a'], { state: { data: { test: false } } }); } </code>
// Click event on "Goto Comp A from Comp B" button
navigate() {
  this.router.navigate(['../comp-a'], { state: { data: { test: false } } });
}

In custom-reuse-strategy.ts,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>shouldDetach(route: ActivatedRouteSnapshot) {
return route.routeConfig?.path === 'comp-a' && (route.data as any)?.test; // I've accessed "test" variable here
}
</code>
<code>shouldDetach(route: ActivatedRouteSnapshot) { return route.routeConfig?.path === 'comp-a' && (route.data as any)?.test; // I've accessed "test" variable here } </code>
shouldDetach(route: ActivatedRouteSnapshot) {
  return route.routeConfig?.path === 'comp-a' && (route.data as any)?.test; // I've accessed "test" variable here
}

I’ve tried this in this stackblitz example.

In the above example, the button Goto Comp A from Comp B is for the Case 2 example and the button Go to Comp B is for Case 1.

How should I modify the route-reuse code or is there any other way to achieve this?

Yes, It should be achieved, but just with some modifications in the stackblitz example.

In app-routing.module.ts

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const routes: Routes = [
{ path: 'comp-a', component: CompAComponent, data: { test: true } },
{ path: 'comp-b', component: CompBComponent },
];
</code>
<code>const routes: Routes = [ { path: 'comp-a', component: CompAComponent, data: { test: true } }, { path: 'comp-b', component: CompBComponent }, ]; </code>
const routes: Routes = [
  { path: 'comp-a', component: CompAComponent, data: { test: true } },
  { path: 'comp-b', component: CompBComponent },
];

In app.component.ts

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>navigateFromHeader() {
this.router.navigate(['comp-a'], { state: { fromHeader: true } });
}
navigateFromBack() {
this.router.navigate(['comp-a'], { state: { fromHeader: false } });
}
</code>
<code>navigateFromHeader() { this.router.navigate(['comp-a'], { state: { fromHeader: true } }); } navigateFromBack() { this.router.navigate(['comp-a'], { state: { fromHeader: false } }); } </code>
navigateFromHeader() {
  this.router.navigate(['comp-a'], { state: { fromHeader: true } });
}

navigateFromBack() {
  this.router.navigate(['comp-a'], { state: { fromHeader: false } });
}

In custom-reuse-strategy.ts,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>shouldDetach(route: ActivatedRouteSnapshot): boolean {
return route.data?.test || false;
}
shouldAttach(route: ActivatedRouteSnapshot): boolean {
const fromHeader = history.state?.fromHeader;
if (fromHeader) {
this.storedRoutes[route.routeConfig?.path || ''] = null;
return false;
} else {
return !!this.storedRoutes[route.routeConfig?.path || ''];
}
}
</code>
<code>shouldDetach(route: ActivatedRouteSnapshot): boolean { return route.data?.test || false; } shouldAttach(route: ActivatedRouteSnapshot): boolean { const fromHeader = history.state?.fromHeader; if (fromHeader) { this.storedRoutes[route.routeConfig?.path || ''] = null; return false; } else { return !!this.storedRoutes[route.routeConfig?.path || '']; } } </code>
shouldDetach(route: ActivatedRouteSnapshot): boolean {
  return route.data?.test || false;
}

shouldAttach(route: ActivatedRouteSnapshot): boolean {
 const fromHeader = history.state?.fromHeader;
  if (fromHeader) { 
    this.storedRoutes[route.routeConfig?.path || ''] = null;
     return false;
  } else {
    return !!this.storedRoutes[route.routeConfig?.path || ''];
  }
}

New contributor

Vaibhav Gupta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Angular: How to block RouteReuseStrategy based on condition?

I’m trying to block the RouteReuseStrategy based on some conditions but this is not happening. I’ve a route name as comp-a which is called in two different ways. 1 time is from a back button and the other time from a button in header.

Case 1:
If there’s something happened in Component A and then user goes to Component B and there the user clicks on back button the data should persist in Component A. This case is achieved perfectly by using RouteReuseStrategy.

Case 2:
If there’s something happened in Component A and then user goes to Component B and there the user clicks on the button from header then when the user lands on Component A, the page should load from start i.e. ngOnInit() should get called.

My issue is for the Case 2 which is not working.

I tried to pass data in the route and access it from ActivatedRouteSnapshot in the shouldDetach() function but this is not helping. I took reference from here.

In app-routing.module.ts,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const route: Routes = [
{ path: 'comp-a', component: CompAComponent, data: { test: true } }
];
</code>
<code>const route: Routes = [ { path: 'comp-a', component: CompAComponent, data: { test: true } } ]; </code>
const route: Routes = [
  { path: 'comp-a', component: CompAComponent, data: { test: true } }
];

In app.component.ts

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// Click event on "Goto Comp A from Comp B" button
navigate() {
this.router.navigate(['../comp-a'], { state: { data: { test: false } } });
}
</code>
<code>// Click event on "Goto Comp A from Comp B" button navigate() { this.router.navigate(['../comp-a'], { state: { data: { test: false } } }); } </code>
// Click event on "Goto Comp A from Comp B" button
navigate() {
  this.router.navigate(['../comp-a'], { state: { data: { test: false } } });
}

In custom-reuse-strategy.ts,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>shouldDetach(route: ActivatedRouteSnapshot) {
return route.routeConfig?.path === 'comp-a' && (route.data as any)?.test; // I've accessed "test" variable here
}
</code>
<code>shouldDetach(route: ActivatedRouteSnapshot) { return route.routeConfig?.path === 'comp-a' && (route.data as any)?.test; // I've accessed "test" variable here } </code>
shouldDetach(route: ActivatedRouteSnapshot) {
  return route.routeConfig?.path === 'comp-a' && (route.data as any)?.test; // I've accessed "test" variable here
}

I’ve tried this in this stackblitz example.

In the above example, the button Goto Comp A from Comp B is for the Case 2 example and the button Go to Comp B is for Case 1.

How should I modify the route-reuse code or is there any other way to achieve this?

Yes, It should be achieved, but just with some modifications in the stackblitz example.

In app-routing.module.ts

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const routes: Routes = [
{ path: 'comp-a', component: CompAComponent, data: { test: true } },
{ path: 'comp-b', component: CompBComponent },
];
</code>
<code>const routes: Routes = [ { path: 'comp-a', component: CompAComponent, data: { test: true } }, { path: 'comp-b', component: CompBComponent }, ]; </code>
const routes: Routes = [
  { path: 'comp-a', component: CompAComponent, data: { test: true } },
  { path: 'comp-b', component: CompBComponent },
];

In app.component.ts

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>navigateFromHeader() {
this.router.navigate(['comp-a'], { state: { fromHeader: true } });
}
navigateFromBack() {
this.router.navigate(['comp-a'], { state: { fromHeader: false } });
}
</code>
<code>navigateFromHeader() { this.router.navigate(['comp-a'], { state: { fromHeader: true } }); } navigateFromBack() { this.router.navigate(['comp-a'], { state: { fromHeader: false } }); } </code>
navigateFromHeader() {
  this.router.navigate(['comp-a'], { state: { fromHeader: true } });
}

navigateFromBack() {
  this.router.navigate(['comp-a'], { state: { fromHeader: false } });
}

In custom-reuse-strategy.ts,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>shouldDetach(route: ActivatedRouteSnapshot): boolean {
return route.data?.test || false;
}
shouldAttach(route: ActivatedRouteSnapshot): boolean {
const fromHeader = history.state?.fromHeader;
if (fromHeader) {
this.storedRoutes[route.routeConfig?.path || ''] = null;
return false;
} else {
return !!this.storedRoutes[route.routeConfig?.path || ''];
}
}
</code>
<code>shouldDetach(route: ActivatedRouteSnapshot): boolean { return route.data?.test || false; } shouldAttach(route: ActivatedRouteSnapshot): boolean { const fromHeader = history.state?.fromHeader; if (fromHeader) { this.storedRoutes[route.routeConfig?.path || ''] = null; return false; } else { return !!this.storedRoutes[route.routeConfig?.path || '']; } } </code>
shouldDetach(route: ActivatedRouteSnapshot): boolean {
  return route.data?.test || false;
}

shouldAttach(route: ActivatedRouteSnapshot): boolean {
 const fromHeader = history.state?.fromHeader;
  if (fromHeader) { 
    this.storedRoutes[route.routeConfig?.path || ''] = null;
     return false;
  } else {
    return !!this.storedRoutes[route.routeConfig?.path || ''];
  }
}

New contributor

Vaibhav Gupta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Angular: How to block RouteReuseStrategy based on condition?

I’m trying to block the RouteReuseStrategy based on some conditions but this is not happening. I’ve a route name as comp-a which is called in two different ways. 1 time is from a back button and the other time from a button in header.

Case 1:
If there’s something happened in Component A and then user goes to Component B and there the user clicks on back button the data should persist in Component A. This case is achieved perfectly by using RouteReuseStrategy.

Case 2:
If there’s something happened in Component A and then user goes to Component B and there the user clicks on the button from header then when the user lands on Component A, the page should load from start i.e. ngOnInit() should get called.

My issue is for the Case 2 which is not working.

I tried to pass data in the route and access it from ActivatedRouteSnapshot in the shouldDetach() function but this is not helping. I took reference from here.

In app-routing.module.ts,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const route: Routes = [
{ path: 'comp-a', component: CompAComponent, data: { test: true } }
];
</code>
<code>const route: Routes = [ { path: 'comp-a', component: CompAComponent, data: { test: true } } ]; </code>
const route: Routes = [
  { path: 'comp-a', component: CompAComponent, data: { test: true } }
];

In app.component.ts

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// Click event on "Goto Comp A from Comp B" button
navigate() {
this.router.navigate(['../comp-a'], { state: { data: { test: false } } });
}
</code>
<code>// Click event on "Goto Comp A from Comp B" button navigate() { this.router.navigate(['../comp-a'], { state: { data: { test: false } } }); } </code>
// Click event on "Goto Comp A from Comp B" button
navigate() {
  this.router.navigate(['../comp-a'], { state: { data: { test: false } } });
}

In custom-reuse-strategy.ts,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>shouldDetach(route: ActivatedRouteSnapshot) {
return route.routeConfig?.path === 'comp-a' && (route.data as any)?.test; // I've accessed "test" variable here
}
</code>
<code>shouldDetach(route: ActivatedRouteSnapshot) { return route.routeConfig?.path === 'comp-a' && (route.data as any)?.test; // I've accessed "test" variable here } </code>
shouldDetach(route: ActivatedRouteSnapshot) {
  return route.routeConfig?.path === 'comp-a' && (route.data as any)?.test; // I've accessed "test" variable here
}

I’ve tried this in this stackblitz example.

In the above example, the button Goto Comp A from Comp B is for the Case 2 example and the button Go to Comp B is for Case 1.

How should I modify the route-reuse code or is there any other way to achieve this?

Yes, It should be achieved, but just with some modifications in the stackblitz example.

In app-routing.module.ts

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const routes: Routes = [
{ path: 'comp-a', component: CompAComponent, data: { test: true } },
{ path: 'comp-b', component: CompBComponent },
];
</code>
<code>const routes: Routes = [ { path: 'comp-a', component: CompAComponent, data: { test: true } }, { path: 'comp-b', component: CompBComponent }, ]; </code>
const routes: Routes = [
  { path: 'comp-a', component: CompAComponent, data: { test: true } },
  { path: 'comp-b', component: CompBComponent },
];

In app.component.ts

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>navigateFromHeader() {
this.router.navigate(['comp-a'], { state: { fromHeader: true } });
}
navigateFromBack() {
this.router.navigate(['comp-a'], { state: { fromHeader: false } });
}
</code>
<code>navigateFromHeader() { this.router.navigate(['comp-a'], { state: { fromHeader: true } }); } navigateFromBack() { this.router.navigate(['comp-a'], { state: { fromHeader: false } }); } </code>
navigateFromHeader() {
  this.router.navigate(['comp-a'], { state: { fromHeader: true } });
}

navigateFromBack() {
  this.router.navigate(['comp-a'], { state: { fromHeader: false } });
}

In custom-reuse-strategy.ts,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>shouldDetach(route: ActivatedRouteSnapshot): boolean {
return route.data?.test || false;
}
shouldAttach(route: ActivatedRouteSnapshot): boolean {
const fromHeader = history.state?.fromHeader;
if (fromHeader) {
this.storedRoutes[route.routeConfig?.path || ''] = null;
return false;
} else {
return !!this.storedRoutes[route.routeConfig?.path || ''];
}
}
</code>
<code>shouldDetach(route: ActivatedRouteSnapshot): boolean { return route.data?.test || false; } shouldAttach(route: ActivatedRouteSnapshot): boolean { const fromHeader = history.state?.fromHeader; if (fromHeader) { this.storedRoutes[route.routeConfig?.path || ''] = null; return false; } else { return !!this.storedRoutes[route.routeConfig?.path || '']; } } </code>
shouldDetach(route: ActivatedRouteSnapshot): boolean {
  return route.data?.test || false;
}

shouldAttach(route: ActivatedRouteSnapshot): boolean {
 const fromHeader = history.state?.fromHeader;
  if (fromHeader) { 
    this.storedRoutes[route.routeConfig?.path || ''] = null;
     return false;
  } else {
    return !!this.storedRoutes[route.routeConfig?.path || ''];
  }
}

New contributor

Vaibhav Gupta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật