首页 > 解决方案 > laravel 和 vue,加载没有预期道具的视图

问题描述

我之前问过一个关于这个的问题,但我仍然被困住并且遇到了一些问题。

现在,如果我访问 site.com/status/12345,那么我将获得我所期望的状态。具有 id (12345) 的路由命中了一个控制器方法,该方法将该 id 传入并在视图中返回它的 json 版本,我将它用作道具并在页面加载时使用它调用函数。

但是,如果我只是去 site.com/status (它应该点击我的索引方法,只返回基本视图),那么我会得到“未定义的变量 entryKey”。

显然,我将控制器中的 entryKey 值添加到我的刀片中,以作为道具传递,如果它有一个 URL 段,这似乎可以工作,但现在我不能只让我的基本视图返回和site.com/status

我该如何解决这个问题,以便site.com/status点击我的 index 方法并site.com/status/12345点击 detail 方法并将 id 作为道具传递?

web.php(路由)

Route::get('/entry/status', 'entry\entryController@index')
    ->name('entry.status');
Route::get('/entry/status/{id}', 'entry\entryController@details')
    ->name('entry.status');

控制器.php

public function index() {

    return view('entry.status');
}

public function details($id) {

    if (request()->wantsJson()) {
        return json_encode(entry::getDetails($id));
    }

    $entryID = json_encode($id);
    return view('entry.status')
        ->with('entryKey', $entryID);
}

status.blade.php

 <entry-dashboard :entry-key="{{$entryKey}}"></entry-dashboard>

细节.vue

props: [
'entryKey'
],
created() {
  this.setDetailWindow(this.entryKey);
},
setDetailWindow(event) {

  this.detailId = event.taskt_id;

  axios.get('/entry/status/' + this.detailId)
    .then(response => {
    ...
    ...
}

标签: phpvue.jslaravel-5.8

解决方案


推荐阅读