在惯性js中重定向,在javascript代码中重定向



我想在创建问题后重定向到单个问题页面

summitQuestion(){
this.question_button = true
axios.post('/api/question/ask', {
'title' : this.question.title,
'body' : this.question.body,
'tags' : this.tags,
'user_id' : this.$page.props.user.id
}).then(response=>{
this.question_button = false
console.log(response.data)

}).catch(error=>{
this.question_button = false
console.log(error.response.data.errors)
if(error.response.data.errors){
this.title_errors = error.response.data.errors.title
this.body_errors = error.response.data.errors.body
}

})
},

我有这个功能,我想在请求成功后重定向我的spa方式,而不需要重新加载页面来质疑单个页面我正在使用惯性js和jetstream我的laravel路由器低于

Route::middleware(['auth:sanctum', 'verified'])->get('/question/{question}', 'AppHttpControllersQuestionController@show')->name('question-single');

只需在惯性上使用visit方法,如下所示。

this.$inertia.visit(route('question-single'), { method: 'get' });

如果你从上面的代码中得到了所有正确的东西,那么在没有重新加载页面的情况下保持重定向,那么我想你的代码的修改将是如下示例;

summitQuestion(){
this.question_button = true
axios.post('/api/question/ask', {
'title' : this.question.title,
'body' : this.question.body,
'tags' : this.tags,
'user_id' : this.$page.props.user.id
}).then(response=>{
this.question_button = false
// console.log(response.data)
this.$inertia.visit(route('question-single'), { method: 'get', data: response.data });

}).catch(error=>{
this.question_button = false
console.log(error.response.data.errors)
if(error.response.data.errors){
this.title_errors = error.response.data.errors.title
this.body_errors = error.response.data.errors.body
}

})

},

你可以通过访问Inertiajs官方网站来参考这一点

如果使用Inertia,则应该创建一个具有链接到字段的v-model的表单。添加一个类型为submit的按钮来调用您的方法(请参阅下面的方法示例(。

<form @submit.prevent="submitQuestion">
<input type="text" v-model="form.title">
<button type="submit"></button>
</form>
<script>
export default {
data() {
return {
form: this.$inertia.form({
title: this.question.title,
body: this.question.body,
tags: this.tags,
user_id: this.$page.props.user.id,
}, {
bag: 'default',
}),
}
},
methods: {
summitQuestion: function() {
this.form.post('/api/question/ask');
},
}
};
</script>

重定向可以直接在控制器方法上完成。

class QuestionController extends Controller
{
public function create(Request $request) {
// Create your question
return redirect()->route('your.route');
}
}

最新更新