gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 22:07:52 24 4
gpt4 key购买 nike

我有下一个 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: 无法读取未定义的属性“post”

我尝试过:

{
// ...
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()
}
// ...
}

但是又报错了:未捕获的类型错误:无法读取未定义的属性“post”

我做错了什么?

最佳答案

让我们看看 mounted() 我认为它有帮助

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

关于Vue.JS:页面加载后如何调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37098229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com