gpt4 book ai didi

vue.js - 方法未在挂载钩子(Hook)中定义,Vue JS

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

所以我正在使用 Laravel 5.3,我正在尝试创建一个 DataTable,我正在尝试创建一个从后端获取数据的方法,并且我正在尝试在组件启动时立即调用它准备好了。

我发现 ready() Hook 现在已失效并被 mounted() 取代,因此我的模板看起来像这样。

<template>
..Simple Bootstrap table...
</template>

<script>
export default {
data: () => {
return {
searchQuery: "",
columns: ['ID', 'Name', 'Campaign', 'Method', 'Limit Per Connection', 'Limit Per Day', 'Active', 'Last Ran'],
lastId: 0,
rowsPerPage: 10,
gridData: [
{ id: 1, name: "Example 1", campaign: 3412, method: "POST", limitConn: 1250, limitDay: 50, active: "Yes", lastRun: "2016-11-27 18:42:21"},
{ id: 2, name: "Example 1", campaign: 3412, method: "POST", limitConn: 1250, limitDay: 50, active: "Yes", lastRun: "2016-11-27 18:42:21"},
{ id: 3, name: "Example 1", campaign: 3412, method: "POST", limitConn: 1250, limitDay: 50, active: "Yes", lastRun: "2016-11-27 18:42:21"},
{ id: 4, name: "Example 1", campaign: 3412, method: "POST", limitConn: 1250, limitDay: 50, active: "Yes", lastRun: "2016-11-27 18:42:21"},
{ id: 5, name: "Example 1", campaign: 3412, method: "POST", limitConn: 1250, limitDay: 50, active: "Yes", lastRun: "2016-11-27 18:42:21"},
{ id: 6, name: "Example 1", campaign: 3412, method: "POST", limitConn: 1250, limitDay: 50, active: "Yes", lastRun: "2016-11-27 18:42:21"},
{ id: 7, name: "Example 1", campaign: 3412, method: "POST", limitConn: 1250, limitDay: 50, active: "Yes", lastRun: "2016-11-27 18:42:21"}
]
}
},
methods: {
/**
* Fetch JSON data for crons from the Backend
*
* @param lastId - The last ID in the current data
*/
fetchData: (lastId) => {
Vue.http.get('/data').success((response) => {

console.log(response);

}).error((response) => {

console.error(response);

})
},
},
mounted: () => {
// When the Component is ready fetch the JSON from the Server Backend
this.fetchData(0);
},
}
</script>

<style>...My Css...</style>

Mounted 方法触发但说 this$1.fetchData is not defined 知道我做错了什么吗? Mounted Hook 是否无法访问我的方法?

最佳答案

mounted 的语法如下:

mounted () {
// When the Component is ready fetch the JSON from the Server Backend
this.fetchData(0);
}

Don't use arrow function for lifecycle hooks ,箭头函数使用词法 this 由它们的上下文决定,vue 将无法为我们绑定(bind)它。

关于vue.js - 方法未在挂载钩子(Hook)中定义,Vue JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40830651/

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