简体   繁体   中英

Import/Export JavaScript can't be used

I'm trying to export & import a JS code and I can not find the right way to do it.

In horizontalScrollFunction.js I have

export function double(n) {
    return n * 5;
}

In home_page.blade.php I have

<script type="module">
    import { double } from "./public/assets/customFunctions/horizontalScrollFunction.js"

    console.log(double(5));
</script>

The error that I get

GET http://mylink/public/assets/customFunctions/horizontalScrollFunction.js net::ERR_ABORTED 404 (Not Found)

Also, my main JS file is imported via VITE

@vite('resources/js/appErp.js')

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';

export default defineConfig({
    plugins: [
        laravel([
            'resources/js/appErp.js',
        ]),
    ],
});

文件夹结构

Desktop:

OS: Windows 10

Browser: Chrome Version 103.0.5060.134

Version Laravel: 9.19

According to export documentation , you forgot to add the * after function in your js file. So your js code should be

export function* double(n) {
    return n * 5;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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