gpt4 book ai didi

laravel - 如何通过laravel axios将id从路由器从一个vue组件传递到另一个vue组件来获取数据

转载 作者:行者123 更新时间:2023-12-03 08:42:00 25 4
gpt4 key购买 nike

Employee.vue组件

<tr role="row" class="odd" v-for="(employee, index) in employees" :key="index">
<td>{{ index+1 }}</td>
<td><router-link :to="/employee-profile/+employee.id">{{ employee.name }}</router-link></td>
</tr>

I am sending employee id from here to EmployeeDetails.vue by routes in app.js


let routes = [
{ path: '/employee', component: require('./components/office/Employee.vue').default },
{ path: '/employee-details/:id', component: require('./components/office/EmployeeDetails').default },
]

这是我的EmployeeDetails.vue组件
<script>
export default {
data() {
return {
employees:{},
}
},
mounted() {
let id = this.$route.params.id

axios.get('api/employee-details/'+id)
.then(response => {
this.employees = response.data;
});
}
}
</script>

这是 api.php 文件,我通过API资源调用了路由
Route::get('employee-details/{id}', 'API\EmployeeController@employeeDetails');

这是我的Controller EmployeeController.php,我在其中调用了返回数据的函数
public function employeeDetails($id)
{
return DB::table('employees')->where('id', $id)->get();
}

But the problem is: Data is not showing and return a error message in my console. Error is given below. Actually I want How can I solve this error.


app.js:81221 [Vue warn]: Error in render: "TypeError: Cannot read property '0' of undefined"

found in

---> <EmployeeProfile> at resources/js/components/office/EmployeeProfile.vue
<Root>
app.js:82484 TypeError: Cannot read property '0' of undefined

最佳答案

您在路由器链接中传递的ID似乎不正确。

它应该是 :

<router-link :to="`/employee-profile/${employee.id}`">{{ employee.name }}</router-link>

关于laravel - 如何通过laravel axios将id从路由器从一个vue组件传递到另一个vue组件来获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60995835/

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