我想從我的控制器訪問變數 $count 并將其訪問到 vue 組件(前端)。目前在我的控制器上有這個代碼:
public function index()
{
$count = User::where('created_at', Carbon::today())->count();
return view('user.index', compact('count'));
}
我對 laravel 和 vuejs 很陌生,所以請幫幫我。謝謝!
uj5u.com熱心網友回復:
將計數作為道具從刀片發送到組件
<your-component-name :count="{{ $count }}"></your-component-name>
并從您的組件中接收該道具
<template>
<div>
Count is {{ count }}
</div>
</template>
<script>
export default {
props: {
count: {
type: Number
}
}
}
</script>
uj5u.com熱心網友回復:
要將資料傳遞給您的組件,您可以使用道具。在此處查找有關道具的更多資訊。這也是定義這些道具的一個很好的來源。
您可以執行以下操作:
<example :userId="{{ Auth::user()->id }}"></example>要么
<example v-bind:userId="{{ Auth::user()->id }}"></example>然后在你的
Example.vue檔案中你必須定義你的道具。然后就可以通過 this.userId 訪問了。喜歡 :
<script> export default { props: ['userId'], mounted () { // Do something useful with the data in the template console.dir(this.userId) } } </script>
上面的問題在下面的問題中得到了回答, 將資料從刀片傳遞到 vue 組件
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/439803.html
