Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get URL query parameters in Vue 3 on Component

i am new to Vue JS, i must say i really love this platform. I started using it just 3 days back. I am just trying to get the URL query parameter and i am using vue-router as well. Here is how i have it:

http://localhost:8001/login?id=1

Here is how my controller look like.

<template>
    <section class="contact-info-area">
        <div class="container">
            <div class="row">
                <div class="col-lg-12">
                    <section class="write-massage-area">
                        <div class="mb-5"></div>
                        <div class="row justify-content-center">
                            <div class="col-lg-5">
                                <div class="write-massage-content">
                                    <div class="write-massage-title text-center">
                                        <h3 class="title">Login {{ $route.query.id }}</h3> <!-- THIS IS WORKING -->
                                    </div>
                                    <div class="write-massage-input">
                                        <form @submit.prevent="onSubmitForm">
                                            <div class="row">
                                            
                                                <div class="col-lg-12">
                                                    <div class="input-box mt-10">
                                                        <input type="text" placeholder="Email"  v-model="form['email']">
                                                        <ErrorMessage :validationStatus="v$.form.email"></ErrorMessage>
                                                    </div>
                                                </div>
                                                <div class="col-lg-12">
                                                    <div class="input-box mt-10">
                                                        <input type="text" placeholder="Password"  v-model="form['password']">
                                                        
                                                    </div>
                                                </div>
                                                <div class="col-lg-12">
                                                    <div class="input-box mt-10 text-center">
                                                        <button type="submit" class="main-btn main-btn-2">Login</button>
                                                    </div>
                                                </div>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="mb-5"></div>
                    </section>
                </div>
            </div>
        </div>
    </section>
</template>

<script>

import { ref } from 'vue'
import useVuelidate from '@vuelidate/core'
import { required, minLength, email } from '@vuelidate/validators'
import ErrorMessage from '../components/ErrorMessage'

export default {
    name: "login",
    components: {
        ErrorMessage
    },
    created(){
    },
    setup(){
        const form = ref({})

        const rules = {
            form: {
                email: {
                    required,
                    email
                },
                password: {
                    required,
                    minLength : minLength(5)
                }
            }
        }
        const v$ = useVuelidate(rules, { form })


        function onSubmitForm(){
            console.log(this.$route.query.id) **<!-- THIS IS NOT WORKING -->**
            v$.value.$touch()
            console.log(form.value)
        }

        return { form, onSubmitForm, v$}
    }
    
}
</script>

here on the above code. On button submit i am going to a function called onSubmitForm, there i am using console.log(this.$route.query.id) but this is giving a below error :

Uncaught TypeError: Cannot read property 'query' of undefined
    at Proxy.onSubmitForm (Login.vue?a55b:84)

Why this is happening? I am seeing that in the vue document as well, they mentioned in the same way. What i am missing in this?

Thank you!

like image 570
user3201500 Avatar asked Oct 07 '20 10:10

user3201500


People also ask

How to get the query string parameter in Vue JS?

You can get the query string parameter simply using Vue.js route. Let’s discuss the following example. Consider you have this path: In the given URL the key is test and the value is yay. Inside the Vue component, you can access the query params by using the key like this: //from your component console.log (this.$route.query.test) // outputs 'yay'

Is it possible to pass query parameters dynamically in vue-router?

If he is not using vue-router than this would indeed be a duplicate, as he will need to use standard JS methods. The problem is that the vue documentation considers coupling components to the router to be undesirable. They recommend passing props but it doesn't seem possible to pass query params dynamically as props.

How do I get a query from the router in Vue?

In the destination component, SearchPage, create a query prop which will receive the query object from the router: props: ['query'], setup (props) { console.log (props.query.q); console.log (props.query.status); } What to do if I do not use vue-router? @aProgger - Start using it. Otherwise, you can use vanilla Javascript

What are URL parameters and how do they work?

id, query, role are parameters passed in the URL. The receiving function can have access to the parameters and values, and have processing logic based on values. URL params are heavily used in some of the modern headless CMS applications (e.g. Strapi).


1 Answers

You can call useRoute to access the query params...

<script setup>
import { useRoute } from 'vue-router'

const route = useRoute()
console.log(route.query)
</script>
like image 170
camwhite Avatar answered Sep 20 '22 13:09

camwhite