Description: When using UI.getCurrent().navigate in a setParameter Method, the content of the page is not being displayed. I wanted to create some kind of fallback if no value for the given parameter has been found.
Minimal reproducible example:
@Route(value = "shop", layout = MainLayout.class)
public class CompanyPage extends Composite<Div> implements HasUrlParameter<String> {
....
@Override
public void setParameter(BeforeEvent beforeEvent, @OptionalParameter String s) {
company = companyRepository.getByCompanyLink(s);
if (company == null) {
//Rerturn to home if no company has been found.
UI.getCurrent().navigate("");
} else {
getContent().add(new H1(company.getName()));
}
}
Expected behaviour: Should show the content of my page i wanted to navigate to.
Actual behaviour: Shows nothing when trying to navigate to an other page then the value given in the Route of the class with the Parameter (example here: Shop). Something fails loading here.
Hello. To change the navigation target during the navigation, you should use the rerouting feature, not UI:navigate. Please see https://vaadin.com/docs/v10/flow/routing/tutorial-routing-lifecycle.html#reroute
To change the navigation target during the navigation, you should use the rerouting feature,
What should I do in generic cases where the logic that wants to do the changing doesn't know whether things happen during navigation or not?
Also, how is the developer supposed to know that they should use another method instead? Could we make navigate() throw with a helpful exception instead of silently doing the wrong thing?
Is there any solution for this? I am doing UI.getCurrent().navigate from the MainView to my HomePage and it doesn't show anything.
@ayash You can use one of the rerouteTo methods in the beforeEvent instance that is passed to setParameter.
I need to call UI.Navigate when checking if I have a service. This doesn't necesserily happen during a flow, so if a service is down whilst it is not needed for that particular flow I don't need to navigate away. This can't be done in the beforeEnter lifecycle part, because it's either check all or nothing.
So UI.Navigate is desired for my use case..
Most helpful comment
What should I do in generic cases where the logic that wants to do the changing doesn't know whether things happen during navigation or not?
Also, how is the developer supposed to know that they should use another method instead? Could we make
navigate()throw with a helpful exception instead of silently doing the wrong thing?