How to trigger a method at page load in vuejs?

2020-05-25 04:09发布

问题:

How can I trigger a method at page load like tab?
for example:

<div id='wraper'>
<!-- div id menu not load -->
<div id="menu">
  <a href='#'>test</a>
  <a href='#'>test</a>
  <a href='#'>test</a>
</div>

<!-- load this content  -->
<div id="content">
konten
</div>
  
</div>  

Thanks

回答1:

For vue >= 2.0 use mounted and for previous version use ready.

vm=new Vue({
  el:"#app",
  mounted:function(){
        this.method1() //method1 will execute at pageload
  },
  methods:{
        method1:function(){
              /* your logic */
        }
     },
})


回答2:

This syntax is working fine:

export default {
    mounted() {
      this.myMethod()
    },
}