简体   繁体   English

Laravel:在JavaScript中使用URL代替路由

[英]Laravel: use an url instead route in javascript

In Laravel 5.4, I need to use an url instead route in a javascript file. 在Laravel 5.4中,我需要使用url代替javascript文件中的路由。

Right now I have a blade file with this code: 现在,我有一个带有以下代码的刀片文件:

@section('after-scripts')
    {{ Html::script("https://cdn.datatables.net/v/bs/dt-1.10.15/datatables.min.js") }}
<script>
    $(function () {
        $('#users-table').DataTable({
            processing: true,
            serverSide: true,
            ajax: {
                url: '{{ route("admin.access.user.get") }}',
                type: 'post',
                data: {status: 1, trashed: false}
            },
            columns: [
                {data: 'id', name: '{{config('access.users_table')}}.id'},
                {data: 'first_name', name: '{{config('access.users_table')}}.first_name'},                ],
            order: [[0, "asc"]],
            searchDelay: 100
        });
    });
</script>

But I want to take off the blade this and save it as a normal .js file in my js assets. 但是我想脱下这个刀片并将其另存为我的js资产中的普通.js文件。 What should I do with the blade helpers like route() and config() ? 我应该如何使用route()config()类的刀片助手?

You can initialize JavaScript variables in the blade file with the output of config and route before importing the script. 您可以在导入脚本之前使用config和route的输出在刀片文件中初始化JavaScript变量。

In Blade Template file 在刀片模板文件中

@section('after-scripts')
    {{ Html::script("https://cdn.datatables.net/v/bs/dt-1.10.15/datatables.min.js") }}
<script>
    var accessurl = '{{ route("admin.access.user.get") }}';
    var config = '{{config('access.users_table')}}';
</script>
<script src="{{asset('path/to/jsfile.js')}}" type="text/javascript"></script>

In JavaScript file 在JavaScript文件中

$(function () {
    $('#users-table').DataTable({
        processing: true,
        serverSide: true,
        ajax: {
            url: accessurl,
            type: 'post',
            data: {status: 1, trashed: false}
        },
        columns: [
            {data: 'id', name: config + '.id'},
            {data: 'first_name', name: config +'.first_name'},],
        order: [[0, "asc"]],
        searchDelay: 100
    });
});

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

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