wp_deregister_script('site-js'); wp_register_script('site-js', get_stylesheet_directory_uri().'/js/site.js', array('jquery'), '0.0.3', true); wp_enqueue_script('site-js');
import axio from 'axios'
I kept getting this error message: Uncaught SyntaxError: Cannot use import statement outside a module
I did some googling and put together this snippet of code in my functions file:
From what I saw this did add the type="module" to my script tag. I could see it when I view page source. One thing I did notice though, was that I didnt see type="module" on the script tag when I inspected element. This also didn't seem to fix the error the message.
My buddy suggested I try this instead of import axios from 'axios';
const axios = window.axios;
For now what I ended up doing was in my page.php I added the script tags there just above the get_footer() function. It looks like this now:
<?php $axsrc = get_stylesheet_directory_uri().'/js/axios/axios.min.js'; $src = get_stylesheet_directory_uri().'/js/ajaxloadmore.js'; ?> <script src="<?php echo esc_url($axsrc); ?>"></script> <script type="module" src="<?php echo esc_url($src); ?>"></script>
I do not like this solution because I like to register and load my scripts from my functions file to be able to manage them from one place.
Does anyone have any suggestions for me that will get this to work? I need to add type="module" to the script tag and I want to register these scripts in my functions.php. How can I do that?