How to use keep-alive in vuejs?

前端 未结 2 708
逝去的感伤
逝去的感伤 2020-12-29 06:44

Basically, I want to keep alive 2 components in router-view and it works, however, I don\'t know if I am doing it correctly.

 

        
相关标签:
2条回答
  • 2020-12-29 07:28

    The only disadvantage is that these components are kept in memory and therefore their state is saved and not reset.

    You also lose lifecycle hooks like created, mounted, etc. since the component is not being rebuilt from scratch anymore. You can replace those lifecycle hooks with hooks that are specific to keep-alive components. For example:

    https://vuejs.org/v2/api/#activated

    Whether keep-alive is a disadvantage or advantage is entirely up to your scenario. If you want to keep the state because you want to switch fast and often between the keep-alive components, it could be an advantage. If you really rely on a clean state through components being built and destroyed, it could be a disadvantage.

    0 讨论(0)
  • 2020-12-29 07:32

    1. We can achieve it by using `

    <keep-alive>
    // Render component inside
    </keep-alive>
    

    ` special tag which has provided by vueJs.

    2. use life cycle hooks:

    a. activated()

    b. deactivated()

    0 讨论(0)
提交回复
热议问题