This is my search component but it throws an unexpected mutation of the "query" prop error, I tried this way but still get the error.
<template> <div class="searchbar"> <input type="text" v-model="query.name" // this is the error class="input" placeholder="Search" /> <div v-if="query.name.length > 3"> <select v-model="query.status" class="select"> // This is the error <option value="alive">Alive</option> <option value="dead">Dead</option> <option value="unknown">Unknown</option> </select> </div> </div> </template> <script> export default { props: { query: String } }; </script> <style scoped> .input { font-family: Roboto; font-size: 16px; height: 56px; border-radius: 8px; width: 326px; padding: 16px 48px; line-height: 150%; border: 1px solid rgba(0, 0, 0, 0.5); background: url("../assets/search.svg") no-repeat scroll 10px center; outline: none; } .input::placeholder { line-height: 150%; color: rgba(0, 0, 0, 0.5); } .select { font-family: Roboto; line-height: 19px; color: rgba(0, 0, 0, 0.6); font-size: 16px; height: 56px; border-radius: 8px; width: 240px; padding-left: 14px; border: 1px solid rgba(0, 0, 0, 0.5); outline: none; background: url("../assets/arrow-up-down.svg") no-repeat scroll 90% center; } select { -webkit-appearance: none; -moz-appearance: none; text-indent: 1px; text-overflow: ""; } .searchbar { display: flex; gap: 20px; margin: 16px auto 61px; } </style>
This is the view character which uses the component to search and pass the query data
<template> <div class="container"> <img src="../assets/characterLogo.svg" alt="logo-character" class="logo" /> <CharacterSearchBar :query="query" /> <p v-if="query.name.length < 4"> Enter 4 characters </p> <div class="cards-container" v-if="query.name.length > 3"> <CharacterCard v-for="character in results" :key="character.id" :character="character" /> </div> <div v-if="error" class="not-found"> Sorry, dont have any result </div> <div v-if="query.name.length > 3"> <button @click="loadMore" class="more-button">Load More</button> </div> </div> </template> <script> import CharacterCard from "../components/CharacterCard.vue"; import CharacterSearchBar from "../components/CharacterSearchBar.vue"; import getData from "../composables/getData"; import { APISettings } from "../api/config"; import { watchEffect } from "vue"; import getFilterResults from "../composables/getFilterResults"; export default { components: { CharacterCard, CharacterSearchBar }, setup() { const { charactersUrl } = APISettings; const { results, info, loadMore } = getData(charactersUrl); const { fetchQuery, query, error } = getFilterResults( charactersUrl, results, info ); watchEffect(loadMore) watchEffect(fetchQuery); return { results, info, loadMore, query, error}; }, }; </script>
These are the errors I encountered
Error "query" property vue/no-mutating-props unexpected mutation
Error "query" property vue/no-mutating-props unexpected mutation
Thank you for your help
You set
v-model
ininput
toquery.name
, which meansquery
when you change the input value time, it will change. Sincequery
is a prop, you can't do this.In addition to @Ian's answer, one solution to fix this warning is to use
get
to create acompulated
property with the valuequery
prop andset
with aemit
event to the parent component to update the parent component's query prop, and inhtml
you can use the calculatedvalue instead ofprop
valueAnd in the parent component of this component you can pass
prop
asv-model:prop="prop"
For example