doulun7739 2018-11-17 02:39
浏览 148

无法使用旧的JS函数与Laravel 5.7刀片模板中的一些jQuery与webpack - 找不到方法

I'm using Laravel 5 Boilerplate that comes with some structure already done.

I want to call a simple JavaScript method from the blade. Here's what I did:

1) Created file called test123.js

content of test123.js:

function test123() {
    $(document).ready(function () {
        console.log('hello from test123');
    });
}

2) I opened the existing app.js and added:

import './test123';

3) I called npm dev run - all green, all OK.

4) In blade index.blade.php I typed:

@push('after-scripts')
    <script>
        test123();
    </script>
@endpush

I went to compiled file:

<script src="http://foo.local/js/backend.js?id=8dbe74e012c3122422da"></script>

My method is there:

enter image description here

however it's not visible.

enter image description here

I guess it's about scopes? I don't want to make it window.test123. Should I? I just want define good old JS / jQuery method and invoke it only when needed in specific templates.

"Modern" JS is not my strong point as I find it very convoluted. I haven't touched default webpack.mix.js.

mix.setPublicPath('public');

mix.sass('resources/sass/frontend/app.scss', 'css/frontend.css')
    .sass('resources/sass/backend/app.scss', 'css/backend.css')
    .js('resources/js/frontend/app.js', 'js/frontend.js')
    .js([
        'resources/js/backend/before.js',
        'resources/js/backend/app.js',
        'resources/js/backend/after.js'
    ], 'js/backend.js')
    .extract([
        'jquery',
        'bootstrap',
        'popper.js/dist/umd/popper',
        'axios',
        'sweetalert2',
        'lodash',
        '@fortawesome/fontawesome-svg-core',
        '@fortawesome/free-brands-svg-icons',
        '@fortawesome/free-regular-svg-icons',
        '@fortawesome/free-solid-svg-icons'
    ]);

Please help me to achieve this simple thing. I know the solution probably is not difficult but I don't know what to do…

Thank you.

Edit 1: I almost forgot. Here's the Boilerplate structure:

enter image description here

Edit 2: User brk requested some more screenshots:

enter image description here

  • 写回答

2条回答 默认 最新

  • duansha8115 2018-11-17 21:23
    关注

    Try exporting the function explicitly:

    export function test123() {...}
    

    and later import it:

    import 'test123' from './test123';
    
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)