【问题标题】:How to pass a PHP variable to Vue component instance in Laravel blade?如何将 PHP 变量传递给 Laravel 刀片中的 Vue 组件实例?
【发布时间】:2017-05-22 01:48:44
【问题描述】:

如何将 PHP 变量的值传递给 Laravel 刀片文件中的 Vue 组件?

在我的示例中,我有一个内联模板客户端详细信息,我从 Laravel 获得此视图,所以现在我想将带有 URL /client/1id 传递给我的 Vue 实例。

我的组件,由Laravel 加载,看起来像:

<client-details inline-template>
    <div id="client-details" class="">
          My HTML stuff
    </div>
</client-details>

并由以下人员安装:

Vue.component('client-details', {
    data(){
        return {
            clientId: null
        }
    },
);

我已经试过了

:clientId="{{ $client->id }"

但它不起作用。

【问题讨论】:

    标签: php laravel vue.js vuejs2


    【解决方案1】:

    您必须使用 Vue 的 props 才能通过标记将属性传递给 Vue 的组件。看看下面的例子:

    <client-details inline-template client-id="{{ $client->id }}">
        <div id="client-details" class="">
              My HTML stuff
        </div>
    </client-details>
    

    在你的 Vue 组件中:

    Vue.component('client-details', {
        props: [
            {
                name: 'clientId',
                default:0,
            }
        ]
    });
    

    现在在你的 Vue 组件的其他部分,你可以通过this.clientId 访问这个值。

    问题详情

    请注意,在 HTML 中,我们在 kebab-case 中编写属性名称,但在 Vue 中,我们在 camelCase 中编写它。更多信息在official docs here

    另外,您在 :clientId="{{ $client-&gt;id }}" 中使用了 Vue 的 v-bind 简写,这意味着 Vue 会将双引号内的任何内容作为 JavaScript 表达式处理,因此在这种情况下您也可能会遇到错误。相反,您应该使用不带冒号的clientId="{{ $client-&gt;id }} 格式。

    【讨论】:

    • 这有什么变化吗?我收到“[Vue 警告]:使用数组语法时道具必须是字符串”错误。编辑:通过替换道具解决:[“clientId”]
    • 这不是有效的代码:&lt;client-details inline-template client-id="{{ $client-&gt;id }}"&gt;,你不能像那样传递 PHP 变量
    • @BillalBEGUERADJ 你能详细说明一下吗?
    • @BillalBegueradj OP 使用了laravel 标签,显然{{ $client-&gt;id }} 是laravel 的blade 模板引擎使用的语法,这里的假设完全正确。
    • 为什么是client-id="{{ $client-&gt;id }}" 而不是clientId="{{ $client-&gt;id }}"。我在任何地方都没有看到client-id。只需clientId
    【解决方案2】:

    我刚刚做了这个,它对我来说非常有用。使用 Laravel 5.4 和 Vue 2.x。

    在我的组件中,为对象声明一个属性(props):

    props: ['currentUser'],

    在我的组件被实例化的刀片页面上:

    &lt;my-component v-bind:current-user='{!! Auth::user()-&gt;toJson() !!}'&gt;&lt;/my-component&gt;

    请注意,在组件中,currentUser 使用 camelCase,但由于 html 不区分大小写,因此您必须使用 kebab-case(因此,“当前用户”)。

    另请注意,如果您使用刀片语法{{ }},则其之间的数据将通过 php htmlspecialchars 运行。如果你想要未编码的数据(在这种情况下你会),那么使用{!! !!}

    【讨论】:

    • 还值得指出:使用单引号将对象传递给Vue组件时将其封装,因为toJson()在JSON实体中使用双引号。
    【解决方案3】:

    对于遇到此线程并且仍然遇到错误的任何人,Mustafa Ehsan 在上面给出了正确的答案,但没有解释。反复试验帮助我看到了它。

    我在 newuser.blade.php 中的组件

    <access-request
           firstname="{{ $newuser->firstname }}"
           lastname="{{ $newuser->lastname }}"
           accessid="{{ $newuser->id }}"
    >
    </access-request>
    

    注意使用的绑定方法非常重要。在上面的组件上,我只写了数据绑定的名称(如 React props),然后将其注册到组件 props 上(见下文)。这就是穆斯塔法在他的答案中写下他的绑定,并且工作正常。另一种更 Vue 传递 props 的方法是使用 v-bind: 或 :,但您必须确保同时使用双引号和单引号:

    :firstname="'{{ $newuser->firstname }}'"
    

    如果没有两个引号,您会收到 Vue 警告。

    AccessRequest.vue:

    <template>
       <div class="component">
            <h3 class="">{{ firstname }} {{ lastname }}</h3>
            <p>Access ID: {{ accessid }}</p>
            <label for="administrator">Grant Administrator Priviledges:</label>
            <input name="administrator" type="checkbox" value="True" class="mb-5"><br>
            <button type="submit" class="btn btn-success">Add</button>
            <button type="submit" class="btn btn-danger">Delete</button>
            <hr>
       </div>
    </template>
    
    <script>
      export default {
        name: "AccessRequest",
        props: ['firstname', 'lastname', 'accessid'],
      }
    </script>
    

    【讨论】:

      【解决方案4】:

      我必须将一些服务器数据传递给我的 Vue 组件,我最终在控制器中创建了一个 $server_data 变量并将其传递到一个 json 脚本标签中。

      在控制器中:

      $server_data = json_encode([
          "some-data" => SomeObject::all()
      ]);
      
      return View::make('myview', compact('server_data'));
      

      在模板中:

      <script type="application/json" name="server-data">
          {{ $server_data }}
      </script>
      <!-- Inserts server data into window.server_data -->
      <script>
          var json = document.getElementsByName("server-data")[0].innerHTML;
          var server_data = JSON.parse(json);
      </script>
      

      在vue初始化中,我放了一个计算属性:

      computed: {
          'server_data': function(){
              return window.server_data;
          }
      }
      

      然后,可以在组件模板中使用server_data.some_object 访问数据。

      这允许传递大量结构化数据而不需要许多 html 属性。另一个好处是数据被 json 编码转义。这避免了包含双引号 (") 的变量的潜在错误,这会弄乱 dom。

      【讨论】:

        【解决方案5】:

        通常将 php 变量传递给 vue 我会这样做

        <component-name :prop-name='@json($variable)'></component-name>
        

        【讨论】:

          猜你喜欢
          • 2021-10-22
          • 1970-01-01
          • 2018-05-15
          • 2019-08-12
          • 1970-01-01
          • 2018-09-26
          • 2021-01-21
          • 2020-09-01
          • 1970-01-01
          相关资源
          最近更新 更多