简体   繁体   English

如何在 laravel 中创建 node_module?

[英]How can I create node_module in laravel?

I create a node module file in my project我在我的项目中创建一个节点模块文件

ressources/js/fc.js资源/js/fc.js

function test(nbp, varElt){
    if(nbp > 1)
        varElt.innerText = nbp.toString();
    else
        varElt.innerText = nbp.toString();
}
module.exports = { test }

In my ressources/layouts/app.blade.php I add my module like that:在我的ressources/layouts/app.blade.php 中,我这样添加我的模块:

<script src="{{ asset('js/fc.js') }}" type="module"></script>

but when in a page section view I want to import my module i have an error但是在页面部分视图中我想导入我的模块时出现错误

ressources/views/tests/test.blade.php资源/视图/测试/test.blade.php

@extends('layouts.app')
@section('content')

    <div class="container">
        <div class="row justify-content-center">
            <div class="card">
                <div class="card-body" style="min-width: 700px;text-align: center">
                    <h2 id="test1"></h2><br>
                </div>
            </div>
        </div>
    </div>
@endsection
@section('script')
    <script>
        import { test } from "../../../public/js/impFc";

        $(function(){   
            const title = document.getElementById("test1") ;
            test.test(1,title)
        })

    </script>
@endsection

My error:我的错误:

Uncaught SyntaxError: Cannot use import statement outside a module未捕获的语法错误:无法在模块外使用导入语句

SOmeone have any idea to resolve this?有人有解决这个问题的想法吗?

In your <script> tag you need to add type="module".在您的<script>标签中,您需要添加 type="module"。 So it would look something like this:所以它看起来像这样:

@section('script')
    <script type="module" >
        import { test } from "../../../public/js/impFc";

        $(function(){   
            const title = document.getElementById("test1") ;
            test.test(1,title)
        })

    </script>
@endsection

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

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