Answer the question
In order to leave comments, you need to log in
How to output data to a vue component that is being used in another component?
I am making a component that will bring users to the main page.
First did
<template>
<layout title="Users">
<div class="container mt-5">
<h2>Laravel Inertia JS Pagination Demo</h2>
<table class="table w-full">
<thead>
<tr>
<th>#ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr v-for="item in usersList.data" :key="item.id">
<td>{{ item.id }}</td>
<td>{{ item.name }}</td>
<td>{{ item.email }}</td>
</tr>
</tbody>
</table>
<Pagination class="mt-6" :links="usersList.links" />
</div>
</layout>
</template>
<script>
import Pagination from '@/Pages/Pagination'
export default {
components: {
Pagination
},
props: {
usersList: Object,
},
}
</script>
Route::get('users', [IndexController::class, 'index'])
->name('users');
public function index()
{
$usersList = User::orderBy('id', 'desc')->paginate(3);
return Inertia::render('Users', [
'usersList' => $usersList
]);
}
<template>
<layout title="Users">
<div class="container mt-5">
<h2>Laravel Inertia JS Pagination Demo</h2>
<table class="table w-full">
<thead>
<tr>
<th>#ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr v-for="item in usersList.data" :key="item.id">
<td>{{ item.id }}</td>
<td>{{ item.name }}</td>
<td>{{ item.email }}</td>
</tr>
</tbody>
</table>
<Pagination class="mt-6" :links="usersList.links" />
</div>
</layout>
</template>
<script>
import Pagination from '@/Pages/Pagination'
export default {
components: {
Pagination
},
props: {
usersList: Object,
},
methods: {
fetch() {
axios.get('/users')
},
},
created() {
this.fetch()
}
}
</script>
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'data')
Answer the question
In order to leave comments, you need to log in
If you make a request through api, then you need not in props but in data
export default {
components: {
Pagination
},
data() {
return {
usersList: null,
}
},
methods: {
async fetch() {
this.usersList = await axios.get('/users')
},
},
created() {
this.fetch()
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question