0

I need to destroy and re-create the root application component. This is my template:

<div id="app">
  {{ num }}
</div>

And this is my code:

 if (app) {
      app.$destroy();
    } else {
      app = new Vue(Object.assign({
        el: '#app',
      }, { data: { num: Math.random() } }))
    }

This code runs on button click, but it doesn't clean the template and refresh the new data I'm passing. Any idea why?

4
  • 1
    You may want to use app.$forceUpdate() Commented Aug 26, 2018 at 13:13
  • If app is set then app.$destroy() will be called, but app = new Vue not because it is in the else block, so it won't be recreated (app.$destroy would not magically empty the variable app). But way do you want to to it this way, instead of updating the data? Commented Aug 26, 2018 at 13:13
  • It's not the full story; I need this functionality. Commented Aug 26, 2018 at 13:24
  • 1
    Why do you need this functionality? Please specify, if you don't all we can do is guess Commented Aug 26, 2018 at 13:26

2 Answers 2

2

I'm pretty sure that your attempt so solve a problem in a way that it should not be solve, or that you use vue in the wrong way.

But to you problem, when you execute this code:

if (app) {
  app.$destroy();
}

Then the documentation state:

Completely destroy a vm. Clean up its connections with other existing vms, unbind all its directives, turn off all event listeners.

So they don't say that the element to wich the vue app is attached to has to or will change. If you want to clean up the DOM then you need to delete the content manually.

After app.$destroy() the app variable will still hold the destroyed vue instance (app wont be magically unset). So another execution of the code will not trigger the else block. You also have to set app to something that evaluates to falsy.

if (app) {
  app.$destroy();
  app = undefined;
}

Now app is undefined so at the next time the if statment is evaluated, the else block would be evaluated and a new instance is created.

Sign up to request clarification or add additional context in comments.

3 Comments

Can you create a plunker, please? Its still doesn't work. The view doesn't change.
@undefined you most cerently didn't restore the content of app to contain the tempalte markup again. Because this fiddle works fine. But still, this is mostlikely the wrong way to solve the problem, not matter what you want to achive.
@undefined that's what you have written in your code. Move the new Vue out of the else block and you won't need to clicks. You're not very informative about what exactly you want to do and why.
0

Your specific use case is not specified in the question, but I assume you want to update something that is not being updated automatically.

To update your component you can use

this.$forceUpdate()

to force a reload of the component.

1 Comment

I need to re-create the app because I am allowing to pass more than data, you can also pass your methods, computed, etc.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.