gpt4 book ai didi

javascript - Vue router 从 vue router 链接获取值

转载 作者:行者123 更新时间:2023-11-30 19:04:05 26 4
gpt4 key购买 nike

我的 table 上有一张 list 。它有一个编辑按钮,如果我单击编辑按钮,它会转到另一个名为 editTeacher 的组件。 .我的问题是如何从表中获取数据并将其传输到我的 editTeacher成分。我使用 axios 从路由获取数据。在 laravel 中是这样的

<a href="/editTeacher/{{$teacher->id}}" class="btn btn-info"><span class="glyphicon glyphicon-pencil"></a> .

我如何在 vue 中实现它?

这是我的代码片段

<table id="myTable" class="table table-hover">
<tbody>
<tr>
<th>ID</th>
<th>Image</th>
<th>First Name</th>
<th>Last Name</th>
<th>Gender</th>
<th>Birthday</th>
<th>Age</th>
<th>Type</th>
<th>Department</th>
<th>Status</th>
<th>Actions</th>
</tr>
<tr v-for="teacher in teachers" :key="teacher.id">
<td>{{teacher.id}}</td>
<td><img style=" border-radius: 50%;" :src="'img/'+teacher.image" height="42" width="42"/></td>
<td>{{teacher.firstname}}</td>
<td>{{teacher.lastname}}</td>
<td>{{teacher.gender}}</td>
<td>{{teacher.birthday}}</td>
<td>{{teacher.age}}</td>
<td>{{teacher.type}}</td>
<td>{{teacher.department_name}}</td>
<td v-if="teacher.status == 1"><span class="label label-success">Active</span></td>
<td v-else><span class="label label-danger">Inactive</span></td>
<td><router-link to="/viewTeacher"> <i class="fa fa-edit"></i></router-link></td>
</tr>
</tbody>
</table>

路线

//Teachers
Route::get('/getTeachers','TeacherController@index');
Route::post('/addTeacher','TeacherController@store');
Route::put('/editTeacher/{id}','TeacherController@update');

app.js 路由

{ path: '/viewTeacher', component: require('./components/editTeacher.vue').default },

最佳答案

Vue js 编辑方法请引用以下代码。根据你的 git 仓库。

app.js 路由

{ path: '/viewTeacher/:id', component: require('./components/editTeacher.vue').default, name: viewTeacher},

Teachers.vue 中的编辑按钮

<router-link :to="{name: 'viewTeacher', params: {id: teacher.id}}" class="btn btn-xs btn-default">Edit</router-link>

EditTeacher.vue 组件

<template>
<div class="row">
<div class="col-xs-3">
<div class="box">
<div class="box-tools">
<img style="border-radius: 50%;" src="" height="100" width="50">
</div>
</div>
</div>
<div class="col-xs-9">
<div class="box">
<form v-on:submit.prevent="saveForm()">
<div class="row">
<div class="col-xs-12 form-group">
<label class="control-label">Teacher first name</label>
<input type="text" v-model="teacher.firstname" class="form-control">
</div>
</div>
<div class="row">
<div class="col-xs-12 form-group">
<label class="control-label">Teacher Last name</label>
<input type="text" v-model="teacher.lastname" class="form-control">
</div>
</div>
<div class="row">
<div class="col-xs-12 form-group">
<button class="btn btn-success">Update</button>
</div>
</div>
</form>
</div>
</div>
</div>
</template>

<script>
export default {
mounted() {
let app = this;
let id = app.$route.params.id;
app.teacherId = id;
axios.get('/getTeacher/' + id)
.then(function (resp) {
app.teacher = resp.data;
})
.catch(function () {
alert("Could not load teacher")
});
},
data: function () {
return {
teacherId: null,
teacher: {
firstname: '',
lastname: '',
}
}
},
methods: {
saveForm() {
var app = this;
var newTeacher = app.teacher;
axios.patch('/editTeacher/' + app.teacherId, newTeacher )
.then(function (resp) {
app.$router.replace('/');
})
.catch(function (resp) {
console.log(resp);
alert("Could not Update");
});
}
}
}

Web.php

Route::get('/getTeachers','TeacherController@index');
Route::get('/getTeacher/{id}','TeacherController@show');
Route::post('/addTeacher','TeacherController@store');
Route::put('/editTeacher/{id}','TeacherController@update');

Controller

public function show($id)
{
return Teacher::findOrFail($id);
}

关于javascript - Vue router 从 vue router 链接获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59177031/

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