When to use the lifecycle method beforeMount in vue.js?

后端 未结 1 455
时光说笑
时光说笑 2020-12-16 05:01

I try to come up with an example when to use each Vue.js lifecycle hook. For beforeMount() I can\'t come up with any use case. While researching I have als read

1条回答
  •  鱼传尺愫
    2020-12-16 05:39

    Best use case that I can come up with comes from Directly injecting data to Vue apps with Symfony/Twig. Before the mount happens, you can still see the actual, untransformed Element before it gets replaced by Vue. A particular piece that you can access is the data properties. In the example below, we lose data-fizz if we don't pull stuff out of it before we get to mounted.

    const app = new Vue({
      el: "#app",
      data() {
        return {
          foo: "bar"
        };
      },
      template: "
    {{foo}}
    ", beforeMount() { console.log(this.$el); //
    console.log(this.$el.dataset.fizz); // buzz }, mounted() { console.log(this.$el); //
    bar
    console.log(this.$el.dataset.fizz); // undefined } });
    
    
    

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