SarahJessica
SarahJessica

Reputation: 524

Vue component defined but not used error message. How do I use it properly?

As a newbie to Vue.js I have defined and, as far as I'm aware, used a nav component, however, npm run serve disagrees.

The exact error I get is:

 ERROR  Failed to compile with 1 errors                                                                                                                                                             20:51:39

 error  in ./src/App.vue

Module Error (from ./node_modules/eslint-loader/index.js):
error: The "nav" component has been registered but not used (vue/no-unused-components) at src/App.vue:13:5:
  11 |   name: 'app',
  12 |   components: {
> 13 |     nav,
     |     ^
  14 |   },
  15 | };
  16 | </script>

1 error found.

 @ ./src/main.js 6:0-28 12:13-16
 @ multi (webpack)-dev-server/client?http://192.168.233.240:8080/sockjs-node (webpack)/hot/dev-server.js ./src/main.js

File layout

src
|
|__assets
|  |__css
|     |__ style.css
|
|__components
|  |__nav.vue
|
|__App.vue
|
|__main.js

App.vue

    <template>
  <div id="app">
    <nav />
  </div>
</template>

<script>
import nav from '@/components/nav.vue';

export default {
  name: 'app',
  components: {
    nav,
  },
};
</script>

main.js The only change I believe I've made is adding in the bootstrap.css file.

import Vue from 'vue';
import App from './App.vue';
import 'bootstrap/dist/css/bootstrap.css';

import './assets/css/style.css';

Vue.config.productionTip = false;

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

And, of course, the template itself.

    <template>
  <nav class="nav flex-column">
    <a class="nav-link active" href="#">Active</a>
    <a class="nav-link" href="#">Link</a>
    <a class="nav-link" href="#">Link</a>
    <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
  </nav>
</template>

<script>

export default {
  name: 'nav',
};
</script>

Upvotes: 1

Views: 14363

Answers (2)

Jesper
Jesper

Reputation: 3834

You shouldn't call it nav since there's already a default HTML component with this name (as you're using yourself in the actual component).
Rename your component to Nav (Yes a capital N), and it should work just fine after this.

Nav.vue

<template>
  <nav class="nav flex-column">
    <a class="nav-link active" href="#">Active</a>
    <a class="nav-link" href="#">Link</a>
    <a class="nav-link" href="#">Link</a>
    <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
  </nav>
</template>

export default {
  name: 'Nav',
};

App.vue

<template>
  <div id="app">
    <Nav />
  </div>
</template>

<script>
import Nav from '@/components/Nav';

export default {
  name: 'app',
  components: {
    Nav,
  },
};
</script>

Upvotes: 3

muya.dev
muya.dev

Reputation: 975

You are getting this error because you cannot name a component with an existing html tag. Nav is a reserved keyword in html. Rename your component differently other than nav to something like app-nav-bar, see below.

<template>
  <div id="app">
    <app-nav-bar />
  </div>
</template>

<script>
import navBar from './components/nav.vue';
export default {
  name: 'app',
  components: {
    'app-nav-bar': navBar
  }
}
</script>

Upvotes: 1

Related Questions