第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將兩個或多個變量從 Laravel 控制器發(fā)送到 vue 組件

將兩個或多個變量從 Laravel 控制器發(fā)送到 vue 組件

PHP
慕勒3428872 2021-12-24 16:03:57
假設我有一個像這樣的 Vue 組件export default {    created() {        axios.get("a url").then(res => {            console.log(res.data);        });    }};然后axios向laravel控制器中的這個函數(shù)發(fā)送請求public function something(){    $data = Model::all();    $comments = Comment::all();    // here i want to send both $data and $comments to that view    return response()->json();}我可以同時發(fā)送它們嗎?這個組件和函數(shù)只是一個例子
查看完整描述

3 回答

?
慕森王

TA貢獻1777條經驗 獲得超3個贊

在這里,您可以創(chuàng)建一個關聯(lián)數(shù)組并通過 json 發(fā)送。


public function something()


    {

        $data=Model::all();

        $comments=Comment::all()

        return response()->json([

         'data'=>$data,

         'comments'=>$comments

       ]) //here i want to send both $data and $comments to that view

    }

在 Vue 中你可以寫這樣的東西。


export default

{

    data(){

       return {

         comments:[]

       }

    },

    created()

        {


           axios.get("a url")

                .then(res=>{

                    this.comments = [...res.data.comments] //If you are using ES6

                    }

        }

}


查看完整回答
反對 回復 2021-12-24
?
智慧大石

TA貢獻1946條經驗 獲得超3個贊

您可以通過使用responselaravel的方法簡單地實現(xiàn)這一點


public function something()

{

    $data=Model::all();

    $comments=Comment::all()

    return response()->json([

         'data'=> $data,

         'comments'=> $comments

       ], 200);

}

或使用相同方法的另一種方式


public function something()

{

        $data=Model::all();

        $comments=Comment::all()

        return response([

         'data'=> $data,

         'comments'=> $comments

        ], 200);

}

在您的組件中,您可以簡單地使用


export default

{

    created()

        {

           axios.get("a url")

                .then(res=>{

                       console.log(res.data.data)

                       console.log(res.data.comments)

                    }

        }

}

謝謝


查看完整回答
反對 回復 2021-12-24
?
德瑪西亞99

TA貢獻1770條經驗 獲得超3個贊

你可以簡單地做到這一點


public function function()


{

    $data=Model::all();

    $comments=Comment::all()

    return response()->json([

     'data'=>$data,

     'comments'=>$comments

   ]) //here i want to send both $data and $comments to that view

}


查看完整回答
反對 回復 2021-12-24
  • 3 回答
  • 0 關注
  • 174 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號