Vlad, nearly there, you pass the second prop just as you did with the first one. You should then bind (v-model) the input to this value, however doing jus that will cause an error (avoid mutating a prop),So use as prop, maybe, initRegion, the data variable you maybe call region and set it to this.initRegion. If you struggle, let me know.
<html>
<head>
<meta charset="UTF-8">
<title>Vue Testing</title>
</head>
<body>
<div id="app">
<autocomplete-region-component
query-prop="regionName"
init-region="1"
>
</autocomplete-region-component>
</div>
<!-- development version, includes helpful console warnings -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
Vue.component('autocomplete-region-component', {
props: {
queryProp: {
required: false,
type: String
},
initRegion: {
required: false,
type: String
}
},
computed: {
val2: {
get() {
return this.otherValue;
},
set(newValue) {
this.$emit('other-input', newValue);
},
},
},
data() {
return {
results: [],
query: this.queryProp,
region: this.initRegion
};
},
methods: {
autoComplete() {
this.results = [];
if(this.query.length > 2){
axios.get('/api/regions',{params: {_limit: 2, query: this.query}}).then(response => {
this.results = response.data;
});
}
},
selectRegion(result) {
let inputWithRegionName = document.querySelector('.js-region-name');
let inputWithRegionId = document.querySelector('.js-region-id');
let listRegions = document.querySelector('.panel-footer');
inputWithRegionName.value = result.name;
inputWithRegionId.value = result.id;
listRegions.hidden = true;
}
},
template:`
<div>
<input
type="text"
placeholder="Начните вводить ваш город и выберите его из списка"
autocomplete="off"
v-model="query"
v-on:keyup="autoComplete"
class="form-control js-region-name"
name="regionName"
value=""
>
<input
type="hidden"
class="form-control js-region-id"
name="regionId"
:value="region">
<div class="panel-footer" v-if="results.length">
<ul class="list-group select-region">
<li class="list-group-item list-region" v-for="result in results" v-on:click="selectRegion(result)">
{{ result.name }}
</li>
</ul>
</div>
</div>
`,
mounted() {
//
}
})
var app = new Vue({
el: '#app',
data: {},
})
</script>
</body>
</html>
You can pass as many values as you want via props, however there is only one "value" that will interact with v-model.
The good news is that v-model is really just a handy abstracted pattern for components.
You could make a second "value" in a component as follows:
<!-- updateV2 method sets v2 to value raised in the event -->
<my-component
v-model="v1"
other-value="v2"
@other-input="updateV2"
></my-component>
computed: {
val2() {
get() {
return this.otherValue;
},
set(newValue) {
this.$emit('other-input', newValue);
},
},
}
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
Vue is an open source JavaScript framework for building a creative interface. Integration with Vue in projects using other JavaScript libraries is simplified because it is designed to be adaptable. Over 36,000 websites currently use Vue. Companies like Stackoverflow, PlayStation, etc. rely on Vue for their UI sites.
https://vuejs.org/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.