Completely unload and reload forge viewer

2019-05-23 15:15发布

问题:

I'm using the Forge Viewer on an Angular 5 application.

Is there a way to completely unload the viewer so it can later be reloaded?

I have the following code to unload the viewer:

if (this.viewer && this.viewer.running) {
  this.viewer.tearDown();
  this.viewer.finish();
  this.viewer = null;
}

And I also have the code to load the viewer every time the user enters the page.

Currently, when a user navigates to a different page on the application, that code gets executed, but when the user comes back to the page that contains the viewer, it shows a grey box where the viewer should be.

Here's a github repo that reproduces the issue: https://github.com/theivanaguilar/forge-viewer-reload

Am I missing something here?

回答1:

The static method Autodesk.Viewing.Initializer can only be called once the subsequent times it will not return. All other viewer methods need to be invoked every time your component loads. So you need to refactor your code a bit.

My app is using React and I save a boolean in the appState to avoid calling that Initializer twice if it has been initialized already.

Hope that helps