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

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (1268)

我有一个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

提问于
用户回答回答于
// 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
   }
}
用户回答回答于

从外部导入该函数,并且没有将其添加到方法块。这样的情况下this不是vm

要么这样做:

ready() {
  checkAuth.call(this)
}

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

methods: {
  checkAuth: checkAuth
},
ready() {
  this.checkAuth()
}

扫码关注云+社区

领取腾讯云代金券

年度创作总结 领取年终奖励