从 Laravel 控制器设置 Vue 数据

Set Vue data from Laravel controller

我是 Vue 新手,我尝试使用 vuejs 将变量从 Laravel 控制器设置为 div。

我已经在 laravel 控制器中设置变量并通过以下方式传递给视图:

$Category = Session::get('Category'); 
//I was previously set that session field and it's string 'category'

return view('index', compact('Category');

我如何在 vuejs 中读取该变量?

您可以简单地将内容作为 属性 传递。如果您的组件有类别 属性

....
props: ['categories'],
...

而且你喜欢

<my-category-example-component :categories="json_encode($Category)"></my-category-example component>

然后您就可以访问您的方法中的信息

methods: {
   test: function() { alert(this.categories); }
}

或在您的模板中

<li v-for="category in categories">{{ cartegory }}</li>