Leveraging Vue.js as a development dependency in a TypeScript project

I am currently working on a .net core project where all client-side scripts are written in TypeScript. I have a desire to incorporate Vue.js into the TypeScript codebase. Below are snippets of the necessary configurations:

tsconfig.json

{
  "compilerOptions": {
    "allowJs": true,
    "target": "es5",
    "outDir": "wwwroot/js",
    "moduleResolution": "node",
    "module": "es2015",    
    "typeRoots": [
      "node_modules/@types"
    ]
  },
  "compileOnSave": true, 
  "include": [
    "TypeScripts"
  ]
}

In my package.json file, Vue is included as a DevelopmentDependency.

{
  "version": "1.0.0",
  "name": "asp.net",
  "private": true,
  "devDependencies": {
    "@types/bootstrap": "3.3.36",
    "@types/jquery": "3.3.22",
    ...
  }
}

While most of my dev dependencies do not require an import statement at the top of TypeScript files due to compilation and bundling, Vue.js behaves differently. In order to utilize Vue.js within TypeScript, adding the import Vue from 'vue' statement is essential. However, this leads to TypeScript compiler errors and runtime exceptions, such as:

Uncaught SyntaxError: Unexpected identifier (as shown here).

The relevant part of my TypeScript file is showcased below:

import Vue from 'vue';

class Exam {
    addExamVueApp : Vue;    
    init() {
        this.initVue();
        this.initCategoryTree();        
    }

    private initVue() {        
        this.addExamVueApp = new Vue({
            el: '#addExamVueAppRoot',
            data: {
                page: 1,
...

Upon further testing, it seems that if there was a way to instruct TypeScript not to include import statements in the generated JavaScript file, the errors would disappear. Removing imports from the JS file resulted in the application functioning without any exceptions.

Answer №1

I successfully resolved the issue by including Vue's path in the typeRoots section of my tsconfig.json file. Interestingly, even for devDependencies, I didn't have to add any import statements.

"typeRoots": [      
  "node_modules/@types", "node_modules/vue"
]

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

What is causing this unique component to be positioned outside of the <tr> tag?

table.vue ... <tbody class="table-body"> <slot></slot> </tbody> ... TableCellRow.vue <template> <td class="table-row-cell" :class="this.class"> <s ...

Obtain an Array Following the For Loop

Struggling with grasping the concept of for loops in arrays. I'm attempting to develop a Thank You card generator and here are the steps I am endeavoring to execute: Initialize a new empty array to store the messages Loop through the input array, con ...

There is no value inputted into the file

I'm facing a small issue while trying to retrieve the value from my input of type="file". Here is the code snippet: <tr ng-repeat="imagenDatos in tableImagenesPunto | filter: busquedaDatosPunto " > <td>PNG</td> <td>{{imag ...

The array is producing accurate results, however it is displaying as undefined in the HTML output

I have encountered a problem while working on my project. I am utilizing an API to fetch an array of platforms where a video game is available. Although the array is returning the correct data, I am facing difficulty in displaying these values in my HTML a ...

Advantages of passing individual variables instead of the entire object from HTML to JavaScript in AngularJS

When it comes to passing the iterating object from HTML to Javascript, there are two approaches that can be taken. The first approach involves passing the iterating object as a whole, while the second approach only passes the required properties of the obj ...

The element 'nz-list-item-meta-title' in NG-ZORRO is unrecognized

After installing NG-ZORRO for my project, I decided to start by incorporating their list component. However, I encountered errors with elements such as: 'nz-list-item-meta-title' and 'nz-list-item-action' not being recognized. I have e ...

Place form at the center of the Bootstrap Modal

My question is, how can I center the entire form, including the input fields and labels, within the modal? Here is also a helpful Fiddle: http://example.com, although the snippet provided should work as well. <script src="https://example.com/bootstr ...

Nuxt - Issue persisting logged in state on refresh despite information being stored in local storage and cookies

I am facing an issue where the state gets cleared on refresh, even though the token, userid, user email, and expiration date are stored in local storage and cookies. I suspect there might be a problem with the store or something else needs to be done to re ...

Changing an element within an item stored in Ionic Storage

Hello, I am currently attempting to update a specific part of an object stored in Ionic storage. The current data in the Storage looks like this: key : object value : {a: "1", b: "2", c: "3"} To modify one of the values to 10, I created the following fu ...

JavaScript Promise Handling: using fetch method to retrieve and extract the PromiseValue

I am currently struggling to access the [[PromiseValue]] of a Promise. However, my function is returning a Promise instead and what I really want myFunction to return is the value stored in [[PromiseValue]] of the promised returned. The current situation ...

Guide on implementing a template within a form using Vue.js

I have set up a Vue instance for handling the form data var formInstance = new Vue({ el: '#amount_form', data: { logdate: '', amount:'', description:'' }, methods: { ...

Encountering the error message "Attempting to access properties of undefined (specifically 'map')". Despite having the array properly declared and imported

I am facing an issue while trying to iterate through an array and display names for each person. Despite declaring the array properly, it is returning as undefined which is causing the .map function to fail. Can you help me figure out where I am making a m ...

What is the solution to the problem "How can you resolve the issue where 'Cannot set property 'ref' of undefined' occurs"?

My goal is quite simple - I just want to retrieve data from Cloud Firestore. Below is the code snippet I am using: import React from 'react'; import firebase from "react-native-firebase"; export default class newsFeed extends React.Component { ...

Sending data from a PHP array to a JavaScript array within a PHP function

I have been scouring Stack Overflow for a solution to my problem, but I haven't found one that fits my specific issue. I recently took over a project from a former coworker that involves managing all the videos and images for my company. My current di ...

Retrieving a specific item using a key in Vue.js

In my Vue template, I have the following data: 0: { id:1, name:test1} 1: { id:2, name:test2} 2: { id:3, name:test3} While the loop <div v-for="(product, index) in products" :key="product.id"> works perfectly, I am wondering how I ...

Tips for storing a JSON file locally and accessing it at a later time on the client side

Can PHP be used to generate a JSON file containing information like first name and last name? When using json_encode, what is the process of saving it on the client side, and how can it be retrieved and read afterward? ...

Translating Encryption from Javascript to Ruby

I have an application which utilizes HTML5 caching to enable offline functionality. When the app is offline, information is stored using JavaScript in localStorage and then transmitted to the server once online connectivity is restored. I am interested in ...

Display a particular element when hovered over

Is there a way to make only individual elements pop up on hover instead of all of them at once? items: [{ item: "Book", info: "Lorem ipsum", displayInfo: false }, { item: "Pen", info: "Lorem ipsum", displayInfo: false }, ...

The type '{ id: string; }' cannot be assigned to the type 'DeepPartial<T>'

In my code, I am attempting to create a generic function that abstracts my repository infrastructure for creating a where clause. export type DeepPartial<T> = T extends object ? { [P in keyof T]?: DeepPartial<T[P]>; } : T; export int ...

Encountering a hydration issue in Next.js when attempting to refresh the page after switching languages (excluding English) with next-translate/useTranslation

I've encountered an issue with the useTranslation hook from the next-translate package in my Next.js project. Although all languages are being recognized, I'm facing a hydration error whenever I change the language and refresh the page. Below is ...