Skip to content
This repository has been archived by the owner on Jul 20, 2023. It is now read-only.

chore : slots in vue js #171

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions source/snippets/vue/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,127 @@ Basic Vue Component Scaffold.
}
</script>
```

### Component with conditional template

```html
<template>
<div id="app">
<div>
Click the button to show/hide the text!
</div>
<p v-if="show">Hey I'm visible</p>
<button @click="toggleShow"></button>
</div>
</template>

<script>
export default {
name: 'App',
data() {
return {
show: true,
};
},
methods:{
toggleShow(){
this.show = !this.show
}
}
}
</script>
```

### Component using Vue 3 Composition API

Basic Vue Component with Vue 3 Composition API.

```html
<template>
<div id="app">
Hello {{name}}!
</div>
</template>

<script>
import { ref } from 'vue';

export default {
name: 'App',
setup(props, context) {
const name = ref('world');
return {
name
}
}
}
</script>

<style>

</style>
```

### Component using Vue 3 Composition API with click event and data binding

```html
<template>
<div id="app">
<button type="button" @click="onClick">Increment (Clicked: {{clicks}})</button>
</div>
</template>

<script>
import { ref } from 'vue';

export default {
name: 'App',
setup(props, context) {
const clicks = ref(0);

function onClick() {
clicks.value++;
}

return {
clicks,
onClick
}
}
}
</script>

<style>

</style>
```

### Slots in Vue

Question : What is slots in vue js ?
Answer : Slots are a mechanism for Vue components that allows you to compose your components in a way other than the strict parent-child relationship. Slots give you an outlet to place content in new places or make components more generic
In other way we can say slots can be use to place any content inside children from parent, let see in above example :

```html
// child.vue
<template>
<div class="child">
<slot></slot>
</div>
</template>
```

Here is simple child component which has slots inside. let's move to the parent component which App.vue,

```html
// app.vue
<template>
<child>
<img src="an-image.jpg">
</child>
</template>
```

So simply we're using child component in App.vue(parent component) & passing it an image element as a slot so
it will render inside child component & replace `<slot></slot>` to the image element.
So we can pass anything through the slots.