Oops! Vue.js is throwing a compile error involving unused variables and undefined variables

I'm currently working on developing a new Vue.js component, but I'm encountering an error when launching the application. The specific error message is displayed below:

https://i.sstatic.net/0MQxl.png

<template>
    <div class="hello" id="app">
         <span style="font-size:30px;cursor:pointer;" onclick="openNav()">&#9776;</span>

        <div id="mySidenav" class="sidenav">
            <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
            <a href="#">{{name1}}</a>
            <a href="#">{{name2}}</a>
            <a href="#">{{name3}}</a>
            <a href="#">{{name4}}</a>

            <div id="bottom">
                <input v-model="name1" style="margin-left: 25px;max-width: 50%;">
                <input v-model="name2" style="margin-left: 25px;max-width: 50%;">
                <input v-model="name3" style="margin-left: 25px;max-width: 50%;">
                <input v-model="name4" style="margin-left: 25px;max-width: 50%;">
            </div>

        </div>
        ...

</style>

Navigation-Drawer.vue


<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png">
    <HelloWorld msg="Welcome to Your Vue.js App"/>
    <App msg="test">
  </div>
</template>

<script>
import HelloWorld from './components/HelloWorld.vue'
import App from './components/Navigation-Drawer.vue'
export default {
  name: 'App',
  components: {
    HelloWorld,
    App
  }
}

...

App.vue

Answer №1

Here are the errors flagged by eslint. For more information, refer to the rules here and here.

To resolve these errors, add the following line just before initializing the app:

// eslint-disable-next-line
var app = new Vue({ 
// additional code 

Ensure that the line containing eslint-disable-next-line is commented out. This instructs eslint to skip over that particular line without linting it.

Answer №2

Upon creating the file main.js with CLI, you will find a code snippet similar to this for initializing a new Vue instance:

new Vue({
  render: h => h(App)
}).mount('#app')

Instead of using mount, it might utilize an el property or reference store or router if included during project setup. The crux is that this is where the primary Vue instance is established. Typically, there is no need to redeclare new Vue, especially within a .vue file.

The App mentioned above should correspond to the component within App.vue, which must be imported earlier in main.js.

Now onto App.vue. This line may seem perplexing:

import App from './components/Navigation-Drawer.vue'
While technically correct, it's recommended to rename App to something more coherent like NavigationDrawer. Remember to update all references including the template.

Moving on to Navigation-Drawer.vue, here are a few issues to address.

Firstly, remove the id="app" from the template as it could result in conflicts.

In order to implement onclick="openNav()" in Vue, opt for @click="openNav" and define openNav in the component's methods section.

Your script section should resemble the following:

<script>
    export default {
        name: 'NavigationDrawer',

        props: {
            msg: String
        },

        data () {
            return {
                name1: 'name 1',
                name2: 'name 2',
                name3: 'name 3',
                name4: 'name 4'
            }
        },

        methods: {
            openNav() {
                console.log('openNav called')
            },

            closeNav() {
                console.log('closeNav')
            }
        }
    }
</script>

With these adjustments, everything should function smoothly.

The linting errors originally stemmed from:

  1. Declaration of unused variable app.
  2. Attempt to invoke new Vue without importing Vue. Generally, proper imports are necessary for access within .vue files.

However, resolving these linting errors was secondary to rectifying the aforementioned critical issues.

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

Tips for converting an Array object to JSON format

After much effort, I have finally managed to format my data as valid JSON with the following Javascript code: var roles = getSelectedRoles(); // returns an Array object /* TODO: Explore better methods for incorporating roles into JSON data */ var rolesSt ...

Organizing and recycling React styled-components and native elements

As a backend engineer transitioning to frontend development, I am currently learning React and exploring styled-components for styling components. However, I am facing challenges in using styled-components with native React components and updating them in ...

Problem with animated scrolling in Jquery

Currently, I am working on a function that aims to smoothly scroll the web to a specific div each time a user scrolls "into" a container div. You can get a better idea of what I mean by looking at my JSFiddle. Everything works perfectly when the user scro ...

In JavaScript, using window["functionName"](arguments) will result in a TypeError message saying that the function does not exist

When trying to execute an AJAX function based on the active tab in my application, everything works smoothly when I trigger the function after specific events. However, I encounter difficulties when attempting to call the function using a dynamically gener ...

React-highlightjs failing to highlight syntax code properly

I'm currently using the react-highlight library to highlight code snippets in my next.js application. However, I've encountered an issue with the code highlighting when using the a11y-dark theme. https://i.stack.imgur.com/ip6Ia.png Upon visitin ...

Executing the outer function from within the inner function of a different outer function

Imagine this scenario: function firstFunction() { console.log("This is the first function") } secondFunction() { thirdFunction() { //call firstFunction inside thirdFunction } } What is the way to invoke firstFunction from thirdFunction? ...

When implementing Critical CSS, the Jquery function fails to execute upon loading

Currently, I am working on optimizing my website at . In an attempt to improve its performance, I decided to implement critical CSS using penthouse. The Critical CSS code can be found here, generously provided by the main developer of penthouse. However, ...

Tips on utilizing these styles as properties for the Vue component

Is there a way I can incorporate these styles as properties for this component? <template> <div> <transition-group name='fade' tag='div'> ... </div> </te ...

Encountered an issue while attempting to install the npm package - it seems that the operation has been

Starting a themed vuejs project involves installing packages and running the file. However, when attempting to install dependencies from the packages, an error occurs. admin@kali:/media/veracrypt1/themeforest-LSerfC0M-skote-vuejs-admin-dashboard-templat ...

TypeScript's type assertions do not result in errors when provided with an incorrect value

We are currently using the msal library and are in the process of converting our javaScript code to TypeScript. In the screenshot below, TypeScript correctly identifies the expected type for cacheLocation, which can be either the string localStorage, the ...

What is the best approach to developing Vue components with unique templates while maintaining the same functionality without duplicating code?

I am working on a project to develop a custom and versatile datatable component. My goal is to be able to adjust the appearance of the datatable on each page, while maintaining consistent functionality. I want to pass data rows into the component and have ...

Having trouble retrieving state values from the store in Vue3

The data sent from index.vue to user.js is saved in the Array users. I confirmed that users received the data from index.vue using localstorage. I then attempted to access the Array users in chat.vue from users.js Unfortunately, I am experiencing issues w ...

Despite being present in the node_modules folder, the ag-grid-vue module appears to be missing

Currently, I am diligently following the Vue.js AgGrid getting started tutorial step by step: https://www.ag-grid.com/vuejs-grid/ However, upon adding the <script> section and saving my progress, an error promptly appears: ERROR Failed to compile ...

Using the Ternary Operator in JavaScript to Dynamically Update HTML Elements with Angular

Is there a way to convert the code below into a ternary operator so that it can be used in an .HTML file? statusChange && statusChange === 'Employed' ? true : employmentStatus === 'Employed'; To clarify, I want to assign the re ...

Serverless Functions in ZEIT Now - Customizing Routes with Parameters

I successfully implemented 4 serverless routes /api/list (GET) /api/add (POST) /api/update/:id (PUT) /api/remove/:id (DELETE) These routes were added to the api/now.json file in the following format: {"src": "/api/list", "dest": "./list.js", "methods": ...

Adjust the node's location in Cytoscape.js

I recently switched from using Cola to fCose in Cytoscape.js for graphing multiple graphs with no connections. With Cola, I was able to manually set node positions by tweaking the layout options. However, with fCose, despite adjusting variables like quali ...

Show only the portion of the image where the cursor is currently hovering

Information in advance: You can check out the current code or view the live demo (hover image functionality not included) at . The concept: I would like to have it so that when I hover my cursor (displayed as a black circle) over the image, only the s ...

Using router-links to wrap cards for seamless navigation to different views

How can I wrap each workout card in a router-link inside this component to change to another view? Below is the code for reference: <template> <div class="card"> <img class="card-img-top" :src="info ...

Encountering a ETIMEDOUT error "ip address" when making a Node.js request

What I'm doing in the code I am currently reading a text file containing approximately 3500 links. Subsequently, I iterate through each link to filter out the relevant ones and make requests to retrieve their status codes, links, and page titles usin ...

The Angular Material md-menu element stubbornly refuses to close when clicked outside, especially if the target element has a fixed position and

I currently have a <md-menu> element implemented in my webpage. By default, the menu will close if clicked anywhere on the page. However, I have noticed that when clicking inside a fixed element with a specified z-index, the menu does not close. < ...