Sharing State with a Secure Route in Vue Router (using the script setup method)

Hello everyone, I'm encountering an issue while trying to send a state to the protected routes in vue-router. The error that I faced mentioned "Discarded invalid param(s) "_id", "dish_name", "description", "img", "ingredients", "instructions", "cooking_time", "chef", "createdAt", "updatedAt", "__v" when navigating." You can find more details on this error at https://github.com/vuejs/router/blob/main/packages/router/CHANGELOG.md#414-2022-08-22.

I am unable to get the params in the protected routes, any insights as to why?

Below are snippets of my code:

The route.js file

/* Your code snippet here */

The function where I am routing to a protected route

/* Your code snippet here */

Answer №1

There are multiple methods to transfer data from one route to another. I will explain two ways to achieve this.

In the first method, you can utilize the props property on the route like so: (Option API approach)

const router = VueRouter.createRouter({
  history: VueRouter.createWebHashHistory(),
  routes: [
    ...,
    {
      name: 'team-members',
      path: ':id',
      component: TeamMembers,
      props: true,
    }
    ,,,,    
  ]
})

and then map the data to the component using the props property:

export default {
    props: ['id']
}

To consume the route data:

<router-link :to="'/teams/' + valuePassToRoute">View Members</router-link>

or

this.$router.push({name: 'team-members', params: { id: value },});

To retrieve the data from the route:

const id = this.$route.params.id;

This provides detailed information about #route, where the value is t1

If you prefer using the query or meta properties, the process is very similar.

To consume the route data:

this.$router.push({ name: 'team-members', params: { id: this.id }, query: { sort: 'asc' }};

To retrieve the data:

const value = this.$route.query.sort;

The second method involves using the Composition API:

<script>
import { useRoute } from 'vue-router';
export default {
    props: ['id'],
    setup() {
        const route = useRoute();
        const value = route.params.id
    }
}
</script>

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

Validation of Numbers and Characters in DevExpress ASP.NET TextBox

When I try to use masking on the textbox for credit card format, I am having trouble entering a hyphen. It seems that the validation does not accept dashes as I only checked for numbers. Any help would be appreciated. Thank you. <script> functio ...

Creating a complete webpage using HTML

Is there a method to load an HTML file and execute the embedded javascript to create a complete HTML page programmatically similar to how browsers do? Edit 1 - I am working on an application where I need to extract data from an HTML page on a remote websi ...

Issue with popup display in React Big Calendar

I'm currently working on a calendar project using React-Big-Calendar, but I've run into an issue with the popup feature not functioning properly. <div className={styles.calendarContainer} style={{ height: "700px" }}> <C ...

Revive the JavaScript library for handling mouse wheel events

Utilizing the wheel-indicator JavaScript library, I am looking to revert the mouse wheel event back to its original state after it was initially set to preventDefault(). Despite attempting to use indicator.setOptions({preventMouse:"false"}) as suggested b ...

What is the process for sharing your website content on Google Blogger?

I am currently developing a dashboard for a small business website and I would like to implement a feature that allows users to post directly to their Blogger blog from the dashboard of their own website. This eliminates the hassle of having to switch betw ...

Find the JavaScript code that selects the previous value chosen

When working with a select in React, I am facing an issue where the console.log is returning the last value selected instead of the current one. For instance, if I select 4 followed by 3 and then 5, the code will display 1 (default value), then 4, and fin ...

NgOnChanges replaces form control value when user inputs text

In my autocomplete form control component: @Component({ selector: 'app-autocomplete', templateUrl: './app-autocomplete.view.html', changeDetection: ChangeDetectionStrategy.OnPush, }) export class AutoCompleteFilterComponent ...

What is the process for redirecting an API response to Next.js 13?

Previously, I successfully piped the response of another API call to a Next.js API response like this: export default async function (req, res) { // prevent same site/ obfuscate original API // some logic here fetch(req.body.url).then(r => ...

jquery function context

I'm having trouble grasping function scope in this scenario. When a button is clicked, it triggers a dialog box with a textarea inside displaying a URL that can be copied for camera setup. <button id="axis-details" onclick="apikey('<?php e ...

Can we display an express view in response to a WebSocket event?

My goal is to display an express view within a websocket event as shown below: socket.on('name', function () { //prompt the client to render a specific express view }); ...

Clicking a button in jQuery to load the Pagemethods

<script type="text/javascript"> $(document).ready(function() { $('#loadbtn').click(function() { // can 't load opts = { title: 'ABCD', series: [{ ...

The press of the Enter key does not trigger the button

I'm facing an issue with a form that has just one field for user input and a button that triggers some jQuery to hide the login form when clicked. However, pressing enter after entering text causes the page to refresh... I'm starting to think th ...

Send form based on the outcome of the ajax request

I have developed a form that triggers an ajax request upon submission to check the validity of the data. My goal is to automatically submit the form if the ajax response confirms the data is valid, and prevent the form submission if the data is invalid. $ ...

The problem arises when Angular's $interval function is not recognized

Despite the possibility of this being considered a duplicate, none of the related topics have provided a solution to my simple date count down directive: class Clock { constructor() { this.restrict = 'AC'; this.replace = true ...

activate a CSS-only modal with JavaScript

Is it possible to trigger a pure CSS modal using JavaScript (jQuery) without the need for a label, so that it activates when a user visits a page? http://jsfiddle.net/h84nubzt/ <label class="btn" for="modal-one">Example</a> <!-- Modal ...

Using jQuery to adjust the input value up and down with buttons

var number = 1; $("#counter input").attr('value', number); $("#increase").click(function(){ $("#counter input").attr('value', number++); }); $("#decrease").click(function(){ $("#counter input").attr('value', number--); }); ...

JavaScript data manipulation: Determining percentage change within a nested JSON structure

Provided is a JSON file structured like this... data =[ { key: "london", values: [ {day: "2020-01-01", city: "london", value: 10}, {day: "2020-01-02", city: "london", value: 20}, {day: " ...

Vue.js components may not always display the data that has been fetched

Within a Laravel view, there is a component where the data in the select tag sometimes doesn't show up. It's quite strange as it randomly displays the data and other times it doesn't. There are no errors being logged in the console or the vi ...

Including a label for an array within an unnamed array in a JSON string

Is there a way to transform the following data: [{"name": "Donald"}, {"name": "George"}] Into this format instead: {MyArray: [{"name": "Donald"}, {"name": "George"}]} I am currently working on a database server that I built using node.js, express, an ...

Receiving a JSON response from express.js via a jQuery get request is not functioning as expected

My goal is to send a JSON value from the back end to the front end of my application. Currently, I am using express.js and all post methods are working perfectly. When a button is clicked in the front-end of my application, I want to receive an invoice nu ...