Vue.createApp({})
.component('click-counter',{
template: "<button @click="count+">{{count}}</button>',
data(){
return {
count:0
}
})
.mount('#app')
component is reusable
index.html
<click-counter>/</click-counter>
so changes on ones component wont affect on others
it is simple compnent
if it is large component
reusable component with props:
props arew custom attributes thar register on component
:name="plan"
props: {
apiUrl:{
type: String,
default:'thesingle',
required:true,
required: true,
},
childApiUrl:{
type: String,
required: false,
},
nested components in vue
component using in another component
there are two ways to rgesiger componetn
local component vs global comppoent
main.js -> registere globally
locally-> at respective vues
https://vuejs.org/guide/essentials/component-basics.html
at induvidual
component is aobject
communication between vue compoents with custom events
we can pas data to child component using props
from child to parent ising emit
v-om:select="selectplan"
f selected it will call select plan component
select method emits custom event
select(){
this.$emit('select',this.name);}
in parent
:select ="selectPlan"
selectplan function
https://vuejs.org/style-guide/
component name
todo-item
twowords
readability
BaseByttin,vue
base or app or v start with it is good practice
page have one side bar at that we give the
tightly coupled component names
if child component linked with parent component, so use it as prefix
parentcomponent + child component
each component go through the inisitlisation step by stp
https://vuejs.org/api/composition-api-lifecycle.html#composition-api-lifecycle-hooks
after instance created created 9 fetch data using api)
after dom elements loaded mounted
vuejs component slots
distribut content to componetns using slots also
<todo-tem > buy bananans </todo-item>
like normal html we are passing data one type of slot above
insid of component
<slot :class={done}"></slot>
<template v-slot:description>
<template #description> </template
vue3 with composition api
<script setup>
import { ref } from 'vue'
const count = ref(0)
</script>
<button type="button" @click="count++">count is {{ count }}</button>
defineProps({
msg: String,
})
<script setup>
import { ref } from 'vue'
setup() {
defineProps({
msg: String,
})
defineEMits(['hithere]
const count = ref(0)
const log = () =>{
console.log('works');
};
const queryBody = reactive({
sort_by: '',
order: '',
page: 1,
per_page: 5,
});
const pageMeta = computed(() => {
const from = (queryBody.page - 1) * queryBody.per_page + 1;
const to =
queryBody.page * queryBody.per_page > totalRows.value
? totalRows.value
: queryBody.page * queryBody.per_page;
return { from, to };
});
watchEffect(() => {
getData(queryBody);
});
return {
queryBody,
rowItem,
};
</script>
diference between anchor tag and router-link
if we compy the same component multiple there is no effect,
https://vueschool.io/lessons/reusable-components-with-props-2
reusable components
if it is repeating so many places we can use component for action
nested components
use one compionetn in antorh compoinent
locally vs global componentns
if we register at another cmponent that is locally
if we register at main.js the we can access from any vue
v-slot
props
store
event
this.$emit
paretnt to child => props
child to parent => emit
component to component event
onclick
@click="$event =>selected =!selected"
const props = defineProps({
status: {
type: String,
required: true,
validator: (value) => {
return ['syncing', 'synced', 'version-conflict', 'error'].includes(
value
)
}
}
})
emit validation or event validation
comoinent name best practivces
multi word
title case ToDoComponent
thenavitaion
<todo-item></todo-item>
https://vuejs.org/style-guide/rules-essential
vue component live scylcle hooks
if u ever access to dom elements we can use like ref
unmounted
it is called whenever tht e componentt removed from dom
planpicker v-if show
unmounted
if we write a counter in compoentn, if we dont remove it will creash our applicaiton
aftetr unmouted clearnterval
vue component slots
https://vuejs.org/guide/components/slots.html#named-slots
https://vuejs.org/guide/components/slots.html#named-slots
at parent
unmounted
mounted
watch
emit
tv show
fetching data
life cycle hooks
suspense
slots
keep alive
composables
social media instagram clone
authenication
pinia ( how we can store state globally)
database relations
uploading photos
pagination ( scalablilty -> scrollabl e pagination)
vue ui library
4
No comments:
Post a Comment