Tips on how to successfully hand over properties in Vue Router

Hey there, I've been working on passing props in Vue routes and have made some progress but it's not quite what I want.

What I have so far

route.js

{
    path: '/register/',
    name: 'register',
    component: require('./views/Login').default,
}

home.vue

<router-link tag="li" :to="{ name: 'register', params: { isRegisteringMe: 'true' }}" class="btn btn-green m-top-20 menu-btn" v-show="!isLoggedIn">Register</router-link>

In register.vue, I can check

console.log(this.$route.params.isRegisteringMe);
which will return true when I click on the Register link.

However, if a user types in the URL directly as website.com/register, this method won't work. I've tried various methods like the ones below with no luck:

{
    path: '/register/:id',
    name: 'register',
    props: { isRegisteringMe: 'hi props'},
    component: require('./views/Login').default,
}

Or

{
    path: '/register',
    name: 'register',
    props: { isRegisteringMe: 'hi'},
    params: { isRegisteringMe: 'hi'},
    component: require('./views/Login').default,
}

I've experimented with different combinations but I'm still struggling to find a solution.

Answer №1

It seems there may be some confusion regarding how params are used in vue-router. Params should actually be included as part of the path.

For example, if the path is configured like this:

{
    path: '/register/:id',
    // ...
}

<router-link :to="{ name: 'register', params: { id: 'abc123', isRegisteringMe:'true' }}">Register</router-link>
will direct the user to register/abc123.

Because the path is set as path: '/register/:id',, the param isRegisteringMe is not defined and will not appear in the URL.

Remember, params should always be a part of the path. Here are a couple of suggestions for making adjustments:

  1. Consider using a URL query for isRegisteringMe. For instance, /register?isRegisteringMe=true (
    <router-link :to="{ name: 'register', query: { isRegisteringMe: 'true' }}"
    ). You can retrieve the value from $route.query.isRegisteringMe

update:

If you want the user to always have isRegisteringMe:true by default, you can implement a redirection in the register component's mounted lifecycle event.

  1. Alternatively, store isRegisteringMe in the app's state, preferably using vuex if applicable. Then, within the register component, retrieve the state and utilize it accordingly.

I hope these suggestions help guide you in the right direction.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

steps to attach click event to row of a-table component in ant design vue

Here is the code snippet for the table I am working on. I have imported a-table from antd. To enable click functionality to route to a details page from this listing page, an additional td can be added. <a-table :columns="companiesColumns" :d ...

Discrepancy in handling dummy data versus database data

Currently, I am diving into the world of Node.js/Express and have encountered a rather peculiar issue. I suspect that at the core of it lies a misunderstanding of how arrays/objects are copied. Despite my research efforts, I seem to be stuck, so any insigh ...

Why am I unable to retrieve data using jQuery and PHP?

I'm working with a PHP form that involves checkboxes: <form action="" method="post" id="CheckBoxForm"> foreach ( $results as $result ) : <input type="checkbox" class="chk" id="check_list[]" value="'.($result->meta_value).&a ...

send another response following the completion of the request.end()

I am facing challenges with writing the correct callback function. The situation involves a user making a request to "/city", which then triggers the server to make a request to a third-party service for data retrieval using http.request(). I successfully ...

Heads up in vue-router 3.5.1: Starting with Vue Router 4, the v-slot API will automatically enclose its content within an <a> tag

When utilizing "vue": "^2.6.12" and "vue-router": "^3.5.1", I am encountering a warning message every time the page reloads: [vue-router] In Vue Router 4, the v-slot API will by default wrap its content with an &l ...

Express not invoking Passport LocalStrategy

I added a console.log statement in the LocalStrategy callback of passport.js, but it never seemed to execute. I am using Sequelize for ORM with MySQL. passport.js const LocalStrategy = require('passport-local').Strategy const passport = require( ...

Ensuring the legitimacy of Rails form submissions

I am encountering an issue with validating a form before submitting it, as the form is being submitted without triggering the jQuery part. <%= form_for(:session,class:"form-signin form-horizontal",:id=> "form",:role=> "form") do |f| %> & ...

Is it possible to utilize v-if and v-for outside of the HTML element?

My task is to assign the entire list to a variable if the condition is met, otherwise I must iterate through the list and assign each element separately. <ul> <li v-if="flag = true"> <myVueComponent v-bind: ...

Issue with adding HTML content to bootstrap modal not functioning as expected

Having trouble displaying an image in a bootstrap modal by using data- attribute within a php foreach loop. The image doesn't seem to appear in the target div. Here's my code snippet: <script> $(document).ready(function(){ $( ".subscri ...

Showcasing the information of a selected row and retrieving data from a database within the same jsp page using ajax technology

I am a beginner in Java and JSP. I have a JSP page that fetches data from a database and displays it in a table. I want to be able to display the details of each row when the user clicks on the row in the same JSP page. Can anyone help me with this? Below ...

Discovering the significance of a function's scope

I'm a bit confused about how the answer is coming out to be 15 in this scenario. I do understand that the function scope of doSomething involves calling doSomethingElse, but my calculation isn't leading me to the same result as 15. function doSo ...

Can a nodeJS script be written to automate selecting options and filling out forms in HTML?

Greetings everyone, I am new here so please excuse me if my formatting is not up to par. Currently, I am tackling a project for an internship and after some extensive research, I am stumped on how to automate certain reports in the next phase. In summary ...

How to retrieve the user-agent using getStaticProps in NextJS

Looking to retrieve the user-agent within getStaticProps for logging purposes In our project, we are implementing access and error logs. As part of this, we want to include the user-agent in the logs as well. To achieve this, we have decided to use getSta ...

Is it feasible to preserve the HTML form content data even after refreshing the page?

Can someone offer a suggestion that doesn't involve using Ajax? I'm wondering if there is a way to achieve this using JavaScript/jQuery or some other method. Here's my issue: When submitting a page, the action class redirects back to the sa ...

The component fails to reflect changes in props

Just starting out with Vue and feeling a bit overwhelmed by the concepts. I'm experimenting with implementing a reusable tab component that I came across online, and opting to use CDN instead of CLI for specific reasons. My Vue version is v3.2.36 Wh ...

Tips for capturing and storing video with HTML5 WebRTC

Before reading the description, make sure to run the code snippet first. This will provide you with a better understanding of the structure. In the 2nd video element, I am trying to record, play, and save video. The issue I'm encountering is that the ...

Looking for a way to utilize JavaScript to extract data from a database? I'm currently utilizing jQuery autocomplete and seeking to exclusively search for values within the database

Currently utilizing jQuery autocomplete <script> $(function() { var availableTags = [ "ActionScript", "AppleScript", "Scheme" ]; $( "#tags" ).autocomplete({ source: availableTags });}); </script> Any s ...

How can I update a specific element within an array of objects in JavaScript based on its reference rather than its index position?

I am dealing with multiple arrays of objects x=[ {a:1}, {b:2}, {c:3}, {d:4} ] y=[ {e:5}, {f:6}, {g:7}, {h:8} ] (etc) and I have a list of references pointing to the objects I need to replace. Instead of having an index into the array, I hold reference ...

Facing problem with Angular 7 when making a GET request for non-JSON data

Currently, I am retrieving JSON data from a URL using the following method: this.http.get('http://localhost:3200/mydata').subscribe(data => { console.log(data); }); The response is in JSON format, and everything seems to be working fine. ...

Navigate through pages using scrollspy without losing your position when returning

Hey all you web geeks out there, I could really use your help solving a little issue. I've been working with Bootstrap to build a website that utilizes scrollspy for navigating different sections of the page using the navbar. The only way I could sto ...