Vue.JS:页面加载后如何调用函数?

我有下一个Vue组件。 Login作为调用登录功能。 checkAuth-正在调用检查页面刷新之间的授权状态。

但是如何checkAuth不按按钮就可以打电话

var GuestMenu = Vue.extend({
     props : ['username','password'],
      template: `
        <div id="auth">
            <form class="form-inline pull-right">
                <div class="form-group">
                    <label class="sr-only" for="UserName">User name</label>
                  <input type="username" v-model="username" class="form-control" id="UserName" placeholder="username">
                </div>
                <div class="form-group">
                  <label class="sr-only" for="Password">Password</label>
                  <input type="password" v-model="password" class="form-control" id="Password" placeholder="Password">
                </div>
              <button type="submit" class="btn btn-default" v-on:click.prevent="sendLoginInfo()">LOGIN</button>
              <button type="submit" class="btn btn-default" v-on:click.prevent="checkAuth()">CheckAuth</button>
            </form>
        </div>`,

        methods: { //hash key-value
          sendLoginInfo : sendLoginInfo, // key (anyname) | value -> calling function name (from separate file) 
          //calling without brackets because we do need return from function, we need just function

          checkAuth : checkAuth // restore authorization after refresh page if user already have session!
        }

          });

我试图称它为App:

App = new Vue ({ // App -- is need for overwrite global var. Global var need declarated abobe all function, because some it's function is calling from outside
   el: '#app',
  data: 
    {
      topMenuView: "guestmenu",
      contentView: "guestcontent",
      username: "",
      password: "",

    }
    ,
  ready: function()
  {
    checkAuth(); // Here

  }

  }

  )

但是,当并非所有组件都已加载时,就好像在调用它,

 function checkAuth()
{
    // we should NOT send any data like: loginData because after refreshing page
    // all filds are empty and we need to ask server if he have authorize session

  console.log("Checking if user already have active session"); 

    this.$http.post('http://127.0.0.1:8080/checkAuthorization').then(function (response) {
         console.log("server response: ", response.data)
....

在这里我得到错误:

authorization.js:69 Uncaught TypeError: Cannot read property 'post' of undefined

我试着做:

    methods: { //hash key-value
      sendLoginInfo : sendLoginInfo, // key (anyname) | value -> calling function name (from separate file) 
      //calling without brackets because we do need return from function, we need just function

    },
    ready()
    {
      checkAuth()
    }

但是再次出现错误: Uncaught TypeError: Cannot read property 'post' of undefined

我做错了什么?

小卤蛋小卤蛋小哥2020/03/12 14:06:20
// vue js provides us `mounted()`. this means `onload` in javascript.

mounted () {
  // we can implement any method here like
   sampleFun () {
      // this is the sample method you can implement whatever you want
   }
}
理查德Near2020/03/12 14:06:20

您可以从主实例外部导入函数,而不必将其添加到method块中。这样的情况下this不是虚拟机。

要么这样做:

ready() {
  checkAuth.call(this)
}

或先将方法添加到您的方法中(这将使Vue this为您正确绑定)并调用此方法:

methods: {
  checkAuth: checkAuth
},
ready() {
  this.checkAuth()
}
Tony米亚2020/03/12 14:06:20

让我们看看mount(),我认为这是有帮助的

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