default view not appearing for index.html unless NgRoutingUsePushState is set to false. Why?

StackOverflow https://stackoverflow.com/questions/22648204

  •  21-06-2023
  •  | 
  •  

سؤال

Consider a simple index.html:

<body>
  <h1>Example</h1>
  <ng-view></ng-view>
...

with the following route initialization code:

void routeInitializer(Router router, RouteViewFactory viewFactory) {
  viewFactory.configure({
    'hello': ngRoute(
        path: '/hello',
        view: 'view/hello.html'),
    'default': ngRoute(
        defaultRoute: true,
        path: '/default',
        view: 'view/default.html')
  });
};

No view is shown when index.html is visited, unless I add

  ..factory(NgRoutingUsePushState,
      (_) => new NgRoutingUsePushState.value(false))

to my AngularModule() initialization, in which case the default view is shown (which is what I want). Why is adding this NgRoutingUsePushState configuration necessary?

هل كانت مفيدة؟

المحلول

It could be a bug with the onPopState mode implementation. For example, onHashChange doesn't fire the event when the page first loads, so there's this line to handle the initial load: https://github.com/angular/route.dart/blob/master/lib/client.dart#L652

When this was implemented onPopState did fire the event on page load, so same hack wasn't necessary, but this might have changed. Unfortunately, there's no test for this in the current test suite.

From: https://developer.mozilla.org/en-US/docs/Web/API/Window.onpopstate

Browsers tend to handle the popstate event differently on page load. Chrome and Safari always emit a popstate event on page load, but Firefox doesn't.

This requires closer investigation for how this is handled in Dart.

TO BE CONTINUED...

Update:

a fix was submitted for route_hierarchical: https://github.com/angular/route.dart/commit/9796e36fb0c6eee98e6754203ea5cfbb740b3121

نصائح أخرى

Adding NgRoutingUsePushState is not necessary. I've added the snippet below to my main controller as a temporary fix until the bug is addressed. Relevant link: https://code.google.com/p/dart/issues/detail?id=18067

import 'dart:js';
import 'dart:html';

class MainController {
  MainController(NgRoutingHelper helper) {
    Event popStateEvent = new Event.eventType('PopStateEvent', 'popstate');
    context['window'].dispatchEvent(popStateEvent);
  }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top