By default place components in the components
directory.
Name components with pascal case.
1.
2├── components
3│ ├── ComponentOne.vue
4│ ├── ComponentTwo.vue
5│ └── ...
6└── App.vueGroup components by function/feature in subdirectories.
1.
2├── components
3│ ├── Navbar
4│ │ ├── NavbarItem.vue
5│ │ ├── NavbarContainer.vue
6│ │ └── ...
7│ └── Sidebar
8│ ├── SidebarItem.vue
9│ ├── SidebarContainer.vue
10│ └── ...
11└── App.vueFor domain specific resources follow this pattern as well.
1.
2├── components
3│ └── Todos
4│ ├── TodosList.vue
5│ ├── TodosForm.vue
6│ └── TodosMeta.vue
7└── App.vueIf there is "one" of something then convention is the prefix with "The".
1.
2├── components
3│ ├── Todos
4│ │ └── ...
5│ ├── TheNavbar
6│ │ └── ...
7│ └── TheSidebar
8│ └── ...
9└── App.vueBase components should begin with V, Base, or App.
1.
2├── components
3│ ├── Todos
4│ │ └── ...
5│ ├── TheNavbar
6│ │ └── ...
7│ ├── TheSidebar
8│ │ └── ...
9│ ├── VButton
10│ ├── VIcon
11│ ├── VTable
12│ └── ...
13└── App.vueConventions helps others onboard quickly. They're community conventions.
If you initialized your project with the default vue create
command
$ npx vue create hello-world
You'll see a project structure like this:
1.
2├── public
3└── src
4 ├── assets
5 ├── components
6 ├── router
7 ├── stores
8 └── viewsYou could then import and use components like so.
1<script setup lang="ts">
2import Todos from '../components/Todos.vue'
3</script>
4
5<template>
6 <main>
7 <Todos />
8 </main>
9</template>If you're using Nuxt
then you don't have to import anything from the components
dir as Nuxt
will do it for you automatically.
1<script setup lang="ts">
2 -import Todos from '../components/Todos.vue'
3</script>
4
5<template>
6 <main>
7 <Todos />
8 </main>
9</template>It's more useful to bind values to a user event though
All HTML tags are supported. div, img, span, textarea, canvas, etc...
The script tag is used for defining JS.
1<script>
2const message = 'Hello Vue'
3</script>
4
5<template>
6 <h1>Hello Vue!</h1>
7</template>With handlebar syntax we use the JS variables inside the template.
1<script>
2const message = 'Hello Vue'
3</script>
4
5<template>
6 <h1>{{ message }}</h1>
7</template>We can handle events by binding an handler using v-on
. We use it with an event we're
listening for. For example v-on:click
1<template>
2 <button v-on:click="console.log('Clicked')">
3 Log clicked
4 </button>
5</template>The @
we saw earlier allows us to listen for or bind to future events.
1<template>
2 - <button v-on:click="console.log('Clicked')">
3 + <button @click="console.log('Clicked')">
4 Log clicked
5 </button>
6</template>The syntax @click
is shorthand for bind v-on:click
1export default {
2 data () {
3 return {
4 - msg: 'Removed'
5 + msg: 'Added'
6 }
7 }
8}1export default {
2 data() {
3 return {
4 msg: 'Focused!',
5 }
6 },
7}1<script setup>
2</script>
3
4<template>
5</template>1<script setup>
2
3</script>
4<template>
5</template>1components
2├── AComponent.vue
3└── BComponent.vue