During my recent testing, I have successfully utilized the below code to work with a component:
<script>
import x from "/path/to/x.json"
</script>
This code snippet effectively loads the json
file into the variable x
.
Now my objective is to dynamically load a local JSON file based on user input from an <input>
element. For example:
<script>
let files ;
function loadJSONFile(){
doSomething(files[0].name) ;
}
</script>
<input type="file" bind:files on:change={loadJSONFile}>
In this scenario, the function doSomething()
will perform a similar task as the initial import
. However, it allows for loading JSON data from any local directory. The concern arises with the relative path provided by files[0].name
through bind:files
, which may not suit our requirement for absolute paths.