【问题标题】:set laravel variable in vuejs在 vuejs 中设置 laravel 变量
【发布时间】:2020-08-05 05:31:27
【问题描述】:

嗨,我有这个 laravel php 刀片代码

<tr v-for='line , id in edited_lines'>
    <td>
        @include(
            'treats.create_search_and_select',
            [
                'search_and_select' => 'user_id[]',
                'value' =>  null,
                'hidden_id' => null,
                'type' => 'all_type_of_accounts',
                'required' => 'required',
                'language' => 'account',
                'hint' => null,
                'policy' => null,
                'show_type' => 'blank',
                'rank' => null,
            ]
        )
    </td>
</tr>

现在我想要的是将值传递给 hidden_​​id 那来自上面的 v-for 像这样..

'hidden_id' => line['anything'],

所以我尝试了这个。

 'value' =>  "@{{line}}",

还有这个

'value' =>  "{{line}}",

上面没有工作.. 那么我如何设置来自&lt;tr v-for='line , id in edited_lines'&gt;的值或hidden_​​id 非常感谢

【问题讨论】:

  • 你做不到。 PHP 在服务器端编译/运行,而您的 Vue 在客户端(浏览器)编译/运行。

标签: php laravel vue.js vuejs2


【解决方案1】:

从 Laravel 到 Vue 共享(全局)变量的主要方式是:

  • 通过窗口将状态变量传递给您的 Vue 应用程序(例如用于用户信息)
  • 将属性传递给您的组件

但是,没有办法将您的 hidden_id 变量从 Vue 共享到 Blade。我建议从你的 sn-p 和表格行的内容制作一个组件,并在 Vue 中处理所有内容,而不是一半在 Vue 中,一半在刀片中。

使用状态变量,您可以这样做:

<html>
<head>
    <script type="application/javascript">
        window.state = {
            my_js_var: '{{ $myPhpVar }}',
        };
    </script>
</head>
<body>

</body>
</html>

或者只是将变量传递给TableRowComponent

TableRowComponent.vue:

<template>
    <tr v-for='item, id in items'>
        <td>
            <TreatsCreateSearchAndSelectComponent :hidden-id="item.value"/>
        </td>
    </tr>
</template>

【讨论】:

    猜你喜欢
    • 2017-05-03
    • 2016-01-06
    • 2015-04-09
    • 2019-07-16
    • 1970-01-01
    • 2021-09-07
    • 2018-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多