我有下一个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
我做错了什么?