Is it possible to retrieve a variable from a geojson file using Vue 3 and Vite?

For my Vue 3 project, I am trying to import a variable from a geojson file. However, when I use

import line from '@static/line.geojson'
, my page goes blank and it seems like Vue stops working.

If I use

import line from '@static/line.json'
, everything works fine. But, I need the geojson format for the rest of my code and I don't want to have two separate files with different extensions.

When I check the console, I see the error:

Uncaught SyntaxError: Unexpected token ':' (at line.geojson?import:2:10)

However, I know there are no errors in my file because I have used it in a Vue 2 project before without any issues.

Do you have any suggestions on how I can make 'line.geojson' work as a single file? I suspect that there might be a plugin I need to install but I am not sure which one.

This is the content of my 'line.geojson' file:

{
   "type":"FeatureCollection",
   "features":[
      {
         "type":"Feature",
         "geometry":{
            "type":"LineString",
            "coordinates":[
               [
                  2.373307391426136,
                  51.02186641516233
               ],
               [
                  2.373339257074004,
                  51.02183268101012
               ],
             // other points go here
            ]
         },
         "properties":{
            "libelle":"Exploitée",
            "geo_point_2d":[
               51.0366532348,
               2.41012862333
            ],
            "mnemo":"EXPLOITE",
            "rg_troncon":1,
            "pk_fin_r":"311+500",
            "pk_debut_r":"304+226",
            "code_ligne":"300000"
         }
      }
   ]
}

Answer №1

In order to resolve JSON files in Vite, you can utilize the @rollup/plugin-json plugin. Simply install the plugin and specify your options, such as the include parameter. Here is a link to the plugin's documentation: https://github.com/rollup/plugins/blob/master/packages/json/README.md

(recommended)solution2:

import j from "@/assets/a.geojson?raw";
console.log(JSON.parse(j));

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

Altering the names of fields in a REST API's response

Greetings! As a newcomer, I might not grasp advanced solutions right away. Let's get to the point: I need to send a response for a REST API. Essentially, what's happening is that I return an object from a method and then the fields of those obje ...

What is the purpose of making a "deep copy" when adding this HTML content?

My canvas-like element is constantly changing its contents. I am attempting to execute a function on each change that captures the current content and adds it to another element, creating a history of all changes. window.updateHistory = func ...

Is it possible to import multiple versions of a JavaScript file using HTML and JavaScript?

After extensive research, I am starting to think that using multiple versions of the same JavaScript library on a single page is not possible (without utilizing jquery Can I use multiple versions of jQuery on the same page?, which I am not). However, I wan ...

The ng-model is not properly syncing values bidirectionally within a modal window

I am dealing with some html <body ng-controller="AppCtrl"> <ion-side-menus> <ion-side-menu-content> <ion-nav-bar class="nav-title-slide-ios7 bar-positive"> <ion-nav-back-button class="button-icon ion-arrow-le ...

The redirection did not occur as no authorization token was detected

I have two Nodejs applications, one for the front-end and the other for the back-end. The back-end app is secured with token access using express-jwt and jsonwebtoken middlewares. My issue is as follows: when I make a request from the front-end to the bac ...

Steps for making a toggle button with Bootstrap

Initially, I developed a web application using HTML, CSS, and JavaScript. Later on, I was requested to recreate it using Bootstrap as well. While I managed to complete the task successfully, I encountered an issue where toggle buttons in the web app revert ...

Strange behavior observed when transclusion is used without cloning

During my experimentation with transclusion, I wanted to test whether the transcluded directive could successfully locate its required parent directive controller after being transcluded under it. The directives used in this experiment are as follows: - Th ...

Is optgroup malfunctioning in IE 10?

My main question is: does optgroup not function properly in IE? The Plnkr code works fine in Chrome but encounters issues in IE 10. I'm trying to find a solution that will work across both browsers. Is this a known problem? In Chrome, I can expand/co ...

The jQuery pop-up fails to activate on the initial click

I have multiple "Buy Now" buttons for different products. If the button is labeled as "sold-out," it should not do anything, but if it's available, it should trigger a jQuery Magnific Popup. Currently, the popup only opens after the second click becau ...

Tips for implementing multi-language URL routing in a Node.js application

I am seeking guidance on how to handle routing for multi-language URLs in Node.js, Currently, I have the following routes set up where each language generates specific routes as shown below. However, with more than 5 languages, efficiency is becoming a co ...

Having trouble with filtering JSON data in AngularJS?

I'm sorry if this question has already been answered. I tried looking for solutions on other websites, but couldn't understand them. I am attempting to filter JSON data within the ng-repeat function, but whenever I try to input something, it does ...

Searching for a column fails to yield any results after altering the value in

Here is a snippet of code that I am working with: <!DOCTYPE HTML> <html lang="en"> <head> <title> Exact matches overview </title> <script type="text/javascript" src="/static/s ...

Develop dynamic interactions with Laravel and Vue.js by creating bindings to multiple class names

In my Vue component, I have a feature that displays a list of items. Each item can be flagged as "active" with a boolean value which is then saved to a database. For example, out of five total items, three could be marked as "active" while the remaining t ...

"Encountering an issue where ng-repeat doesn't reflect updates after adding a new value to the array. Attempted to use $scope.$

Whenever the chat is opened and a name is clicked, it triggers $scope.openChat(user) which adds a name to the $scope.chat.openChats array. The ng-repeat is supposed to monitor this array for any new values but does not update. I attempted using $scope.$app ...

Spring Controller not able to resolve JSON response

I am facing a similar issue as described here but I have not been able to resolve it using the solutions provided. In my Spring application XML, I have only included the mvc annotation driven element: <mvc:annotation-driven /> Here is the code sni ...

Implementing child components rendering in a React application using TypeScript

Just a little background information: I am attempting to build a carousel with pagination using ReactJS. Here is the code snippet I currently have: interface HTMLCarouselT { children: Array<JSX.Element> size: number; } const HTMLCarousel = ({ch ...

What is the best way to pass my request data to my $scope variable?

I'm currently facing a challenge with this particular topic. My goal is to add the response data that I retrieve from Express to my angular $scope and then direct the user to their profile page. This is how my Controller Function is structured: $sc ...

Vue3 "Trying to access a variable that has not been defined on the current instance during rendering"

Whenever I compile my Vue3 code using Vite, I encounter the following error: runtime-core.esm-bundler.js:38 [Vue warn]: Property "tasks" was accessed during render but is not defined on instance. at <Home onVnodeUnmounted=fn ref=Ref< u ...

The image is experiencing difficulty loading from the Express static directory

Having some trouble with image loading... I've noticed that images are loading fine from the local folder, but not from the uploads folder which is set to be static. I've been attempting to upload a file from the browser. The upload and save pr ...

Output text in Javascript (not to the console)

I'm on the lookout for a way to welcome a user by their name when they enter it into a JavaScript application. I have tried this code snippet: function getname() { var number = document.getElementById("fname").value; alert("Hello, " + fnam ...