Javascript 如何在vuejs中的页面加载时触发方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35064845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:13:34  来源:igfitidea点击:

How to trigger a method at page load in vuejs?

javascriptjqueryvue.js

提问by b4dQuetions

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

谢谢

回答by Ashutosh Raj

For vue >= 2.0 use mountedand for previous version use ready.

对于 vue >= 2.0 使用mounted和对于以前的版本使用ready.

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

回答by DevonDahon

This syntax is working fine:

此语法工作正常:

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