简体   繁体   中英

How to dynamically import a javascript array from a URL?

I have this code:

import("./static.js") .them (m => {...})

where m is some static array defined in static.js How to I accomplish this?

import("https://somesite.com/public/static.js")
.them (m => {...})

because this gets me a Module not found exception There has got to be a simple answer without having to set up a server to serve up the file via REST.

I've found the answer, which is to use fetch:

fetch(url).then (res => res.text())....

As for the inevitable CORS related issue fetching from a remote website, I added this to the NGINX server configuration on that website:

location / {
.......
add_header "Access-Control-Allow-Origin"  *;

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