want to share my routes between my actix-web backend and Vue w/ Vue-Router frontend without having to have to files describing the routes. So when I define the routes in the frontend, I don't want to change anything for the server. I don't know if there's a better solution, if so, please tell me :D
My routes.jsonc is in my src folder. Every route is of the following type:\
route: { path: String; name: String; componentName: String }
Then my routes-Strings are converted to the actual Components:
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,
});
Now the problem is the 4th line: I've tried multiple import statements, also:
import routesImport from "../routes.jsonc";
import routesImport from "src/routes.jsonc";
import routesImport from "@/routes.jsonc";
But I always get "Cannot find module '../routes.jsonc' or its corresponding type declarations" as a typescript and Compiler error. Sometimes also '"default" is not exported by "src/routes.jsonc", imported by "src/plugins/router.ts".'
Now to my question: How can I import a json file without putting it in the public folder as some other questions suggest? I don't want to make this file public if possible.
Are there better alternatives? How could I import this json file?
resolveJsonModuletotrueinside the compiler options in thetsconfig.json? Also you need to make sure, that vite support the.jsonc` extension which is not the case with the default setup: vitejs.dev/config/shared-options.html#resolve-extensions