Importing a JSON or JSONC file into a vite/typescript project can be easily done

I am looking for a way to seamlessly share my routes between my actix-web backend and Vue with Vue-Router frontend without needing separate route files. I want to define the routes on the frontend without having to make any changes on the server side. If there is a better solution out there, please let me know! 😄

In my src folder, I have a routes.jsonc file where each route follows this structure:


    route: { path: String; name: String; componentName: String }

These route strings are then converted into actual Components using the following code snippet:


    import { createRouter, createWebHistory } from "vue-router";
    import ButtonArrayVue from "@/components/Home/ButtonArray.vue";
    import App from "@/App.vue";
    import * as routesImport from "../routes.jsonc";
    
    const history = createWebHistory();
    
    const routes = routesImport.map((route: { path: String; name: String; componentName: String }) => {
        let component = function () {
            switch (route.componentName) {
                case "ButtonArray":
                    return ButtonArrayVue;
                case "App":
                    return App;
            }
        };
        return {
            path: route.path,
            name: route.name,
            component: component,
        };
    });
    
    export default createRouter({
        history,
        routes,
    });

The issue arises when trying to import the JSON file with various attempts resulting in TypeScript and compiler errors. How can I import the JSON file without having it in the public folder and avoid making it accessible to the public?

Are there any alternative methods or better approaches to importing this JSON file?

Answer â„–1

Ensure that you specify resolveJsonModule as true in your tsconfig.json; however, keep in mind that this setting will exclusively apply to files with a .json extension and not those with a .jsonc extension.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Arrange a JSON response in descending order and filter out specific values

Currently, I'm encountering a challenge when trying to extract a specific attribute from my JSON response. The issue arises when I attempt to sort the results based on the `percentage_match` in descending order. Once sorted, my goal is to create an ar ...

I would like to have text show up instead of an alert when validation is triggered

Hey there, I need some help with JavaScript as I'm just getting started. Basically, I want to display the text "please fill in" next to a textbox if the user hasn't entered any information. <!DOCTYPE html> <head> <title>Easy ...

Having trouble accessing HikVision Web SDK functions in Angular 17

First and foremost, this concerns the HikVision cameras specifically. Because the SDK is compiled to plain JavaScript (with jQuery included), I had to import the JS files in angular.json and use declare in the component TS file (in this instance, camera.c ...

What is the best way to adjust a map to completely fill the screen?

I am experiencing an issue with my Openlayer map not fitting to full screen automatically. Despite trying various settings, I am unable to resolve this issue. Can anyone suggest what might be causing this problem? Thank you in advance https://i.stack.imgu ...

Ensure every individual input field in a Bootstrap form is validated using JavaScript before submitting the form

As a newcomer to Javascript, I am seeking assistance with validating each field in the form below without having to click on the submit button. Your help is greatly appreciated! <form action="" id = "form1" class = "form-group row"> & ...

Retrieve the external JSON file using AngularJS

Attempting to retrieve JSON data using Angular JS has been successful with static data. However, encountering issues when trying to fetch data from an external JSON file. Below is the code and corresponding output: index.html <!doctype html> <ht ...

Using the jquery slider in conjunction with the onchange event

I have integrated a jquery slider add-on into my project that updates a value in a Linux file whenever it is manipulated. The slider is connected to a text input box, which is set as readonly and therefore always blurred. The issue I am facing is that the ...

What is Angular's approach to handling a dynamic and unprocessed JSON object?

When a JSON file is placed under assets, accessing it using something like http://localhost:4200/myapp.com/assets/hello.json will fetch the JSON file directly without any graphical user interface. This indicates that Angular must be able to return a raw JS ...

How to display JSON data in a table using UI5

I am encountering an issue while trying to display JSON data in UI5. I have successfully loaded the data and can access it to show in individual fields, such as a text view. However, I am facing problems when attempting to display the data in a table. Her ...

What is the most effective way to extract "sub" values from a Python dictionary?

How can I effectively retrieve values from a Python dictionary? The full function and dictionary code are provided below Context Upon researching, it seems that using the get() method should work. However, my current code only returns "None" for the "ti ...

A guide on triggering a function in Angular 6 when scrolling up or down

Is there a way to trigger a function when a user scrolls up or down in an Angular 6 project? I want to implement a feature similar to fullpage.js in my Angular 6 application. I initially attempted to achieve this using Angular animations, but without succ ...

Delete the text in MUI's TablePagination component that displays the number of rows per page and the total rows in the table

Currently, I am integrating MUI's tablePagination component into my React table with TypeScript. I am looking to remove/disable the circlemarked text displayed in the image (the picture is an example from MUI). https://i.stack.imgur.com/ib0t2.png Af ...

Vue Framework 7 incorporates a validation feature that ensures successful outcomes

In my current project using Framework7 Vue with version 4.4.3, I am facing a challenge in validating a form upon submission. I came across this helpful code snippet: $$('.save').on('click', function(e){ e.preventDefault(); if ...

Can jQuery help me identify which <input type='image> was clicked within a form containing several submit images?

Consider this hypothetical scenario: <form> <input type="text" name="some_name" value="val" id="some_name"> <input type="image" value="action" alt="Add To Cart" name="add" src="/images/submit.gif"> <input type="image" value="act ...

The instance cannot be accessed by ES6 class methods

Having trouble accessing the this instance in one of my methods within a class that I created. In my router, I am calling the method like this: import Emails from '../controllers/emails' import router from 'express' .... route.post(&a ...

Issue in Vuetify: The value of the first keypress event is consistently an empty string

I need to restrict the user from entering numbers greater than 100. The code snippet below represents a simplified version of my production code. However, I am facing an issue where the first keypress always shows an empty string result. For example, if ...

ngif directive does not function properly when subscribe is utilized in ngOnInit

this is the unique component code import { Component, OnInit } from '@angular/core'; import { Subscription } from 'rxjs'; //import { Item } from '../item/item.model'; import { CartItem } from './cart-item.model'; imp ...

Arranging elements in a list according to their position on the canvas using AngularJS

I am currently working on drawing rectangles on an html5 canvas using the JSON format provided below. My goal is to sort the array based on the x and y locations of each element. { "obj0": { "outerRects": [ { "outerRectRoi": { "x1": 0, " ...

Using npm link with Webpack results in eslint errors

I have a multi-package project setup, where I have one JavaScript package that depends on a TypeScript library. Initially, I was using Sinopia and had to reinstall the TypeScript library every time I made changes to it. Then I discovered npm link and thoug ...

What is preventing me from implementing my JavaScript event on my EJS code?

Looking to add a click event to the image in my EJS file, here's the EJS code snippet: <div class="container" id="comments"> <div class="row"> <div class="col-lg-12"> <div class="well"> ...