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
P粉5178143722024-01-01 15:50:58
You set v-model
in input
to query.name
, which means query
when you change the input value time, it will change. Since query
is a prop, you can't do this.
P粉2162035452024-01-01 00:32:09
In addition to @Ian's answer, one solution to fix this warning is to use get
to create a compulated
property with the value query
prop and set
with a emit
event to the parent component to update the parent component's query prop, and in html
you can use the < code>calculatedvalue instead of prop
value
<template> <div class="searchbar"> <input type="text" v-model="compQuery.name" // this is the error class="input" placeholder="Search" /> <div v-if="query.name.length > 3"> <select v-model="compQuery.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 }, computed: { compQuery: { get () { return this.query }, set (value) { this.$emit('update:query', value) }, }, }, }; </script>
And in the parent component of this component you can pass prop
as v-model:prop="prop"
For example
<child v-model:query="query" />