in vs code
create one html file
shift 1
tab
it wil create automatic vue boiler plate
install live server in vs code
open ur html page
right click
open with live server
apps( quiz,notes,crud,ecommerce)
counter
varia
in javascript
variable declarations => const thing,
we can cal it also state
if state change =>
vuejs
===
====
npm init vue@latest
our applcation is depenedent on these dependenicies
to run our application we have ot install this application
it maybe with boiler plate or after completed our application "dependencies": {
after installation of boiler plate
we have to run npm install
then it will download from cloud to our machine
whatever downloaded packages we can see at node_modules
after install
npm run serve or npm run dev
whatever commands run that will be there in packages.json scripts
<button >+</button>
add lisetener to this button may be action the button
<button @click="function name">+</button>
"" => in quotes => strings
v-on:click
on + click => it will update the count means re render that respective html ( in javasript for every rerender => it slow down the page)
const count =0 or const count = ref(0);
console.log(count);
click event handler
<script setup > in compositon api
<script> in options api
its is old one
compositon api
looks cleaner script
pure script or pure javascript
it is future ( use this only)
what is directives
directives are instructions for vue to do certain things
@click ="showmodal=true"
v-show="showmodal"
v-modal =>
bidirectional directive
means it takes from user input and
it takes from varaible
v-for key is
one unique may be id
style with colon means its changing na forevery loop
this is the reason
error handling
errormessage.value="showing error"
notes page
<script setup> import {ref} from "vue"; const showModal = ref(false) const newNote = ref(""); const errorMessage = ref("") const notes = ref([]); function getRandomColor() { return "hsl(" + Math.random() * 360 + ", 100%, 75%)"; } const addNote = () => { if(newNote.value.length < 10) { return errorMessage.value = "Note needs to be 10 characters or more" } notes.value.push({ id: Math.floor(Math.random() * 1000000), text: newNote.value, date: new Date(), backgroundColor: getRandomColor() }); showModal.value = false; newNote.value = "" errorMessage.value = "" } </script> <template> <main> <div v-if="showModal" class="overlay"> <div class="modal"> <textarea v-model.trim="newNote" name="note" id="note" cols="30" rows="10"></textarea> <p v-if="errorMessage">{{errorMessage}}</p> <button @click="addNote">Add Note</button> <button class="close" @click="showModal = false">Close</button> </div> </div> <div class="container"> <header> <h1>Notes</h1> <button @click="showModal = true">+</button> </header> <div class="cards-container"> <div v-for="note in notes" :key="note.id" class="card" :style="{backgroundColor: note.backgroundColor}" > <p class="main-text">{{ note.text }}</p> <p class="date">{{ note.date.toLocaleDateString("en-US") }}</p> </div> </div> </div> </main> </template> <style scoped> main { height: 100vh; width: 100vw } .container { max-width: 1000px; padding: 10px; margin: 0 auto } header { display: flex; justify-content: space-between; align-items: center; } h1 { font-weight: bold; margin-bottom: 25px; font-size: 75px; } header button { border: none; padding: 10px; width: 50px; height: 50px; cursor: pointer; background-color: rgb(21,20,20); border-radius: 100%; color: white; font-size: 20px; } .card { width: 225px; height: 225px; background-color: rgb(237, 182, 44); padding: 10px; border-radius: 15px; display: flex; flex-direction: column; justify-content: space-between; margin-right: 20px; margin-bottom: 20px; } .date { font-size: 12.5px; font-weight: bold; } .cards-container { display: flex; flex-wrap: wrap; } .overlay { position: absolute; width: 100%; height: 100%; background-color: rgba(0,0,0,0.77); z-index: 10; display: flex; align-items: center; justify-content: center; } .modal { width: 750px; background-color: white; border-radius: 10px; padding: 30px; position: relative; display: flex; flex-direction: column; } .modal button { padding: 10px 20px; font-size: 20px; width: 100%; background-color: blueviolet; border: none; color: white; cursor: pointer; margin-top: 15px } .modal .close { background-color: rgb(193, 15, 15); margin-top: 7px; } .modal p { color: rgb(193, 15, 15); } </style>
component and props
import data from data.json
const quizzes = ref(q);
const search = ref("");
it is search state
if it is not empty=> we have to run the login
means continuosly monitoring the state changes or not
for that we have to import watch or compouted
if it is large logic => we have to use watch
watch(search,()=>{
console.log("hello");
});
watch(search,()=>{
quizes.value=data.filter(quiz=>quiz.name.tolowercase().includes(search.value.tolowercase);
});
import {defineProps} from vue;
const {quiz} = defineProps(['quiz]);
console.log(quiz);
pagination and routing
quizzes
click on section
answer questions
redirect to if correct all are corect
at css file
@import "./base.css";
app.vue
import {routerview} from 'vue-router";
<routerview/>
router link has
inbuilt prop is
active-class="active"
dynamic routes
extract the route params
import {useroute} from vue-router;
const route = useROUTE();
route.params.id;
nested routes
car/21/contact
children
404 not found
path:"/:catchall(.*)*"
https://router.vuejs.org/guide/essentials/dynamic-matching
{ path: '/:pathMatch(.*)*', name: 'NotFound', component: NotFound }, // will match anything starting with `/user-` and put it under `route.params.afterUser` { path: '/user-:afterUser(.*)', component: UserGeneric }, ]
redirect
{
path:"home"
redirect:"/",
}
quiz app
const questionstatus = ref(`${current.value}/${variable}`); ( decalared state)
watch(()=>currentquesitonindx.value,()=>{questionstatus.value=`${current.value}/${variable}` });
(listener)
above equalt to computed
const questionStatus = computed(()=>{
return '${curentquestionindex.value}/${quiz.questions.length}';
});
const questionStatus = computed(()=> '${curentquestionindex.value}/${quiz.questions.length}');
instead of declaring state and definign wathc function, we can write as computed function if we wanna observer changes
more computing
const questionStatus = computed(()=> '${curentquestionindex.value/quiz.questions.length}');
child to parent emit
const emti =defineEmits('selectOPtin');
const emtiselectedoption=(argument)=>{}
api
front end put a request to api and it will process this request and fetch data from database
https://breakingbadapi.com/api/characters
https://breakingbadapi.com/api/episodes
https://breakingbadapi.com/api/quote/random
const fetchcarhater = async()=>{
const response = await axios.get('');
}
<suspense> is a inbuitl component
default is top level async
fallback is loader
async =. if loading it showis loader, if loads,it gets the data
npm i -D naiviui
like boostrap vue
https://www.naiveui.com/en-US/os-theme/components/notification
watch(page,async()=>{
const res= await axios.get();
characters.value = res.data;
});
https://vuejs.org/api/composition-api-lifecycle
onuodated means
state is update
which life cycle hook we showuld use
onbeforemounted
onupdated
computed
mounted
rickandmortyapi.com
https://rickandmortyapi.com/documentation
<keepalive> is inbult component
rerender automatically
states in vue
declare state with reactive
const color = reactive({read:0,});
ref is type any => object, boolean
reactive type object it takes object only
ref access state with .value()
declared numbers state in app.vue, passing from paretn to greate grend child step by step via props
https://vuejs.org/guide/reusability/composables
reusability
But what if we want to reuse the same logic in multiple components? We can extract the logic into an external file, as a composable function:
=
create folder called composables (like mixins.js )
composables/usenumbers.js
h
And then you use it in a component:
main.js
written in stores and import where we required likestate management (option api )
No comments:
Post a Comment