Guide on importing CDN Vue into a vanilla Typescript file without using Vue CLI?

In the midst of a large project that is mostly developed, I find myself needing to integrate Vue.js for specific small sections of the application. To achieve this, I have opted to import Vue.js using a CDN version and a <script> </script> tag in the HTML.

Initially, working with JavaScript and utilizing the imported Vue.js posed no issues. However, in a bid to enhance code quality and ensure scalability, I am looking to switch to Typescript from JavaScript, thus creating a roadblock.

It's worth mentioning that Vue CLI is not an option for me as I only require Vue in minor segments of my app.

After exhausting various attempts, I have hit a snag in getting it to function. For the sake of brevity, I will simplify my problem. Here are all the files in my possession:

The file structure includes:

 -index.html
 -vue.js
 -ts/
   -generated/
   -/node_modules/
   -index_javascript.js
   -index_typescript.ts
   -package.json
   -tsconfig.json
   -webpack.config.production.js

Utilizing index_javascript.js with Vue poses no difficulties; everything functions smoothly. However, replicating the same functionality with Typescript in the file index_typescript.ts proves to be challenging.

Here is the content of index_javascript.js:

const myApp = new Vue({
  data() {
    return {
      msg: "Hello",
    };
  },
  methods: {
    // Annotation required due to `this` in return type
    greet() {
      return this.msg + " world";
    },
  },
});

myApp.$mount("#app");

Here is the content of index_typescript.ts:

import { Vue } from "../vue";

const myApp = new Vue({
    data() {
        return {
            msg: "Hello",
        };
    },
    methods: {
        // Annotation required due to `this` in return type
        greet() {
            return this.msg + " world";
        },
    },
});

myApp.$mount("#app");

Here is the content of index.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="./vue.js" defer></script>
    <script src="./ts/index.js" defer></script>
  </head>
  <body>
    <div class="" id="app">
      <p class="">My number is: {{ greet() }}</p>
    </div>
  </body>
</html>
... (Other file content continues)

Answer №1

To resolve this issue, I recommend opting for the npm installation of Vue rather than using CDN or script imports

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

Ensure that the background view remains interactive while adding an overlay on top for an enhanced user experience

Hey everyone, I could use some help with a question I have. My issue is that I am struggling to figure out how to make two views overlap while still allowing the background view to be interactive. Currently, I am using absolute positioning for the foregr ...

Combining multiple Float32Arrays to create a single Float32Array

I need help creating a function that can flatten multiple Float32Arrays into one large Float32Array const first = new Float32Array([1,2]); const second = new Float32Array([3,4,5]); const third = new Float32Array([6,7,8,9]); const chunks = [ ...

React TypeScript throws an error when a Socket.IO object is passed to a child component and appears as undefined

Currently, I'm developing a simple chat application as part of my university course. The code below represents a work-in-progress page for this project. While I am able to get the socket from the server and use it in the main component without any is ...

"Enhance your website with Express.js and eliminate the need for full

As I continue to work on my website, I am faced with a challenge. While the page is not overly large, I want to ensure that when navigating to different tabs in the navbar, the entire site does not have to reload each time. Currently, I am using express.js ...

Guide on sending two dynamically generated select box values through ajax in order to retrieve values for a third select box

I am currently working on a task where I need to populate the 3rd select box based on the selections made in the first two select boxes, which have dynamic values. Below is the jQuery ajax code snippet that I have written (Please note that this code curre ...

Looking to install nodemon for Node.js on macOS? If you're encountering a "command not found" error, here's

After installing nodemon using the command npm install -g nodemon, I encountered a Permissions issue. To resolve this, I used the sudo npm install -g nodemon command. However, when attempting to run the "nodeman" command, I kept receiving an error that s ...

Error encountered when trying to use the .find function in Typescript: "The expression is not callable."

Environment Details: typescript 4.5.5 Error Summary An issue occurred stating: "This expression is not callable. Each member of the union type '{ <S extends User>(predicate: (this: void, value: User, index: number, obj: User[]) => value ...

Displaying JSON data in HTML proves to be a challenge

I have collected JSON data and organized it into two arrays, then displayed it in JSON format. Now I'm trying to showcase this data in HTML using two loops by correlating the IDs of one array with the userIds of another array. {"personaldetails":[{"i ...

When the user presses either the refresh button or the back button, they will be redirected

After the user presses refresh or uses the browser back button, I need to redirect them to a specific page in order to restart the application. My JavaScript code is as follows: var workIsDone = false; window.onbeforeunload = confirmBrowseAway; function ...

Using an external script to modify or call a Vue.js method

My Vue app is constructed using Webpack and includes a few basic computed properties, such as calculating the sum amount from input values. However, I now require the capability to replace the summation function with one stored in a separate file that is n ...

JPlayer experiencing technical difficulties on Internet Explorer and Opera browsers

Hey there! I'm facing an issue with my website . I've uploaded some ogg and m4a files for streaming, and while it works perfectly on Firefox, Safari, and Chrome, it's not functioning properly on IE. When I try to play a song, it seems to loa ...

The MatTableDataSource provides a promise that includes approximately 7000 rows of data

When attempting to load a large amount of data into a MatTableDataSource, I am facing an issue. I would like to display a loader until the data is fully set, but I am unsure of when that happens. I attempted to use a promise like this: return new Promise(r ...

When I make a post request, I receive a response in the form of an HTTPS link, but it is not redirected to

I'm making a post request and receiving the response as follows: " [Symbol(Response internals)]: {url: 'https://login.somenewloginpage'}" My intention is to open a new page using that URL, but unfortunately it does not redirect t ...

The function cannot be called on params.getValue()

I am facing an issue while trying to retrieve the value of a specific row's column ID. The goal is to change the URL by appending the retrieved ID when clicking on an icon. However, I encountered an error stating TypeError: params.getValue is not a fu ...

What causes the input field to lose focus in React after typing a character?

Currently utilizing React Mui for components and encountering no errors in either the Chrome inspector or terminal. How can this be resolved? No error notifications are being displayed by eslint or Chrome Inspector. The form submission functions correctl ...

What is the best way for me to show comments beneath all of my posts?

Currently, I am facing an issue with my application where I need to retrieve comments from posts. I am unsure of the best approach to tackle this problem. The existing system only displays posts that have comments, but it shows only a single comment inste ...

What are some alternative methods for organizing folder structure in Express Handlebars when managing views?

Is there a more efficient way to render HTML files without constantly needing them to have different names? I'm looking for a method where handlebars can identify which file in which folder to render, without encountering conflicts with files of the s ...

Nested arrays in the JavaScript programming language

Is this JavaScript code accurate: var options = []; options.push(["Tom","Smith"]); options.push(["Mary","Jones"]); where each item in options is a two-element array of strings. I plan to add items to options using a for loop. Furthermore, can I i ...

Reactjs: When components are reused, conflicts may arise in UI changes

I'm currently working on creating a sample chat room application using reactjs and redux for educational purposes. In this scenario, there will be 3 users and the Message_01 component will be reused 3 times. Below is the code snippet: const Main = Re ...

Using an external npm module in TypeScript can result in the tsc output directory being modified

In my TypeScript project, I have set up the build process to generate JavaScript files in the ./src/ directory. Everything works smoothly when building against existing npm modules, such as Angular 2 imports. However, I encountered a strange issue when I ...