简体   繁体   English

如何在vuejs路由器链接中传递laravel变量?

[英]How to pass laravel variable in vuejs router link?

I'm trying to pass laravel variable in Vuejs router link but it is not working.How i can pass user id in router link?我正在尝试在 Vuejs 路由器链接中传递 laravel 变量,但它不起作用。我如何在路由器链接中传递用户 ID?

<ul>

    @foreach($children as $child)

        <li>
            @if($child->user_type == 'user')

                <script>
                    var UserId = '{{ $child->id }}';
                </script>

                <router-link :to='"/dashboard/" + UserId' class="nav-link-text mb-1">
               {{ $child->user_name}}</router-link>

            @endif

        </li>

    @endforeach

</ul>

Keep in mind that your blade views will always be constructed, executed and completed before your Vue application is started on the client, so, just like @Alec Joy mentioned in his comment, it'll be easier to directly add the ID in the link.请记住,您的刀片视图始终会在您的 Vue 应用程序在客户端启动之前构建、执行和完成,因此,就像@Alec Joy在他的评论中提到的那样,直接在链接中添加 ID 会更容易.

Your blade code will end up generating:您的刀片代码最终将生成:

<ul>
    <li>
        <script>
            var UserId = '1';
        </script>

        <router-link :to='"/dashboard/" + UserId' class="nav-link-text mb-1">Bob</router-link>

    </li>
</ul>

In this case, the UserId is superfluous, since you are assigning a Javascript variable to be used in one spot.在这种情况下,UserId 是多余的,因为您正在分配一个要在一个地方使用的 Javascript 变量。 It is better to have, in this case, the User ID hardcoded into the routerlink from the blade file.在这种情况下,最好将用户 ID 从刀片文件硬编码到路由器链接中。

Change your blade file to:将您的刀片文件更改为:

<ul>
@foreach($children as $child)
    <li>
    @if($child->user_type == 'user')
        <router-link :to="/dashboard/{{ $child->id }}" class="nav-link-text mb-1">{{ $child->user_name}}</router-link>
    @endif
    </li>
@endforeach
</ul>

Handling dynamic data can be done in two ways.可以通过两种方式处理动态数据。

  1. As URL Path variables作为 URL 路径变量
  2. As Query parameters作为查询参数
  3. As Parameters over the browser history ( a hidden mechanism )作为浏览器历史记录中的参数(一种隐藏机制)

Passing data to components as URL path variable or Query Params将数据作为 URL 路径变量或查询参数传递给组件

https://techformist.com/url-params-vue/ https://techformist.com/url-params-vue/

Try this if you want to have a dynamic data passed to the component over the route link using params (a hidden mechanism).如果您希望使用 params(一种隐藏机制)通过路由链接将动态数据传递给组件,请尝试此操作。

https://dev.to/aligoren/passing-data-to-a-router-link-in-vuejs-2cb0 https://dev.to/aligoren/passing-data-to-a-router-link-in-vuejs-2cb0

Hope this helps.希望这可以帮助。

Orread more on Vue documentation: Dynamic Routing .或者阅读更多关于 Vue 文档的信息:动态路由

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM