ngx-monaco-editor: ModelService: Cannot add model because it already exists
ERROR Error: Uncaught (in promise): Error: ModelService: Cannot add model because it already exists!
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 2
- Comments: 15
@CoderPoet Write URI as Unique names then it will fix
I found the same error.
I create an editor in one of my components, when navigate away and come back, I see this in the console.
And in my template:
As mentioned by @viccsjain, I’m not currently disposing of the model. Unfortunately I was not able to find the right way to do it in the documentation. What would it be the correct way of doing it?
@CoderPoet this error mostly comes when we dispose the editor without disposing model. Could you please provide more details ? Are you disposing editor and then trying to create again ?
@ajaybabum this was my problem as well. I was initializing multiple instances and the
uri
s were not unique. Thanks.@xocoatzin For our situtation (not multiple editors, but routing back and forth to an editor), I’ve done a couple of “hacks” to get around it.
The main one is to simply force a browser refresh if it fails to load. Not very elegant because then the user has to download your whole app again:
The second fix reduces the amount of times this is necessary by using “sticky routing” (when a user leaves a route, the components aren’t destroyed and are re-used when they return - this means the editor doesn’t get recreated):
A new class,
CustomRouteReuseStrategy
:And then in your
app.module.ts
:This seems to fix the majority of navigation failures, and, when it doesn’t, the first “hack” kicks in to refresh the page.
Not ideal at all, but manageable.