Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在laravel和vue js中调用要查看的数据时出错_Javascript_Laravel_Vue.js - Fatal编程技术网

Javascript 在laravel和vue js中调用要查看的数据时出错

Javascript 在laravel和vue js中调用要查看的数据时出错,javascript,laravel,vue.js,Javascript,Laravel,Vue.js,我尝试使用vue js和laravel在视图中显示一些表数据: 以下是我尝试过的: 这是注释控制器: public function index() { $comment = Comment::Latest()->paginate(10); return new CommentResource($comment); } 这是我的vue js评论脚本 export default { data: function() { return {

我尝试使用vue js和laravel在视图中显示一些表数据: 以下是我尝试过的: 这是注释控制器:

 public function index()
{
    $comment = Comment::Latest()->paginate(10);
    return new CommentResource($comment);
}
这是我的vue js评论脚本

  export default {
    data: function() {
        return {
            comments: {}
        }
    },
    created() {
        this.loadComments();
    }
    ,
    methods: {
        loadComments(){
        axios.get("../api/comment").then(
            ({ data })=>(this.comments - data.data)
            // response => this.comments = response.data
        );
        },
    }
}
最后是vue html的html部分

 <div v-for="comment in comments" >
 {{ comment.title }}
 </div>
这里呢

[Vue warn]: Property or method "comment" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

顺便说一句,我肯定我有这个,因为这是我的api,我收到的

当我记录这个时:

  axios.get("../api/comment").then(({ data }) => (console.log(data)))
我得到这个结果:
您已经在从响应中提取数据了。因此,要么像这样使用响应对象:

axios.get(“../api/comment”)。然后((response)=>(this.comments=response.data.data));
或者提取数据属性并使用它

axios.get(“../api/comment”)。然后(({data})=>(this.comments=data.data));

这是因为axios返回包含服务器响应的
data
属性的。由于您的服务器响应还有一个要使用的
data
属性,因此您希望使用response.data.data

您已经从响应中提取了数据。因此,要么像这样使用响应对象:

axios.get(“../api/comment”)。然后((response)=>(this.comments=response.data.data));
或者提取数据属性并使用它

axios.get(“../api/comment”)。然后(({data})=>(this.comments=data.data));

这是因为axios返回包含服务器响应的
data
属性的。由于您的服务器响应还有一个要使用的
data
属性,因此您希望使用response.data.data

因此,老实说,我不明白我提取的部分是错误的或显示的,因为仍然使用您的代码,我在console上出现了相同的错误,并且没有显示数据。您可以记录结果吗
axios.get(“../api/comment”)。然后(({data})=>(console.log(data);this.comments=data))由于语法错误,我对您私用的代码做了一些更改,所以请检查编辑的问题,我想这意味着我得到了数据,但我没有正确显示!!好的,那么就是这样:
axios.get(“../api/comment”)。然后({data}=>(this.comments=data.data))我编辑了答案事实上axios返回一个响应对象,该对象具有一些属性,如数据、状态、标题等。。data属性包含服务器发送的响应(在您的示例中,分页也包含data属性)。所以response是axios响应,response.data是服务器响应,response.data.data是分页数据。。。看,所以我不明白,老实说,我提取的部分是错误的,或者显示的部分是错误的,因为仍然使用您的代码,我在consol上得到了相同的错误,并且没有显示数据。您可以console.log结果吗
axios.get(“../api/comment”)。然后(({data})=>(console.log(data);this.comments=data))由于语法错误,我对您私用的代码做了一些更改,所以请检查编辑的问题,我想这意味着我得到了数据,但我没有正确显示!!好的,那么就是这样:
axios.get(“../api/comment”)。然后({data}=>(this.comments=data.data))我编辑了答案事实上axios返回一个响应对象,该对象具有一些属性,如数据、状态、标题等。。data属性包含服务器发送的响应(在您的示例中,分页也包含data属性)。所以response是axios响应,response.data是服务器响应,response.data.data是分页数据。。。看见
TypeError: Cannot read property 'title' of undefined
{"current_page":1,"data":[{"id":1,"title":"asd","body":"asd","user_id":1,"user_email":"asd","status":1,"created_at":null,"updated_at":null}],"first_page_url":"http:\/\/localhost:8000\/api\/comment?page=1","from":1,"last_page":1,"last_page_url":"http:\/\/localhost:8000\/api\/comment?page=1","next_page_url":null,"path":"http:\/\/localhost:8000\/api\/comment","per_page":10,"prev_page_url":null,"to":1,"total":1}
  axios.get("../api/comment").then(({ data }) => (console.log(data)))