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

https://i.sstatic.net/qr7WA.png

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

What is the process for linking an HTML document to another HTML document within a div using jQuery?

Check out my HTML code: <!DOCTYPE html> <html> <head> <title>Hilarious Jokes!</title> <meta charset="utf-8"> <link href="final%20project.css" rel="stylesheet"> <script src=" ...

Enigmatic void appears above row upon removal of content from a single item

When I click on an item in my grid, the content of that item is moved to a modal. The modal functions properly, but I noticed that when the content is removed from the item, a space appears above it. I have found that using flexbox could solve this issue, ...

Vue - when multiple parents share a common child component

Is there a way in Vue.js for multiple parents to share the same child component? I am looking to have multiple delete buttons trigger a single modal with different content. For example: myfile.html: <table id="app" class="table table-striped table-s ...

Vue instance with non-reactive data

I am looking to store an object in Vue that will be accessible throughout the entire instance but does not need to be reactive. Typically, if I wanted it to be reactive, I would use 'data' like this: new Vue({ data: myObject }) However, since ...

Troubleshooting 404 Error When Using Axios Put Request in Vue.js

I'm trying to update the status of an order using Axios and the Woocommerce REST API, but I keep getting a 404 error. Here's my first attempt: axios.put('https://staging/wp-json/wc/v3/orders/1977?consumer_key=123&consumer_secret=456&apos ...

Is the Spring MVC ModelAndView object being returned?

After clicking a link on a jsp page, I have a GET method that is instantiated as: HTML: <div class="panel accordion clearfix" id="dispdir"> <script type="text/javascript"> window.onload = function() { //showDirectorySe ...

The dom does not automatically update when data is modified inside the watch function

Here is the data within a component: data: function () { return { sltAreaStyle: { paddingTop: "3%", }, checkedTypes: ["krw", "btc", "usdt"], }; }, Next, we have the watch function for the checkedTypes d ...

Unlocking the Potential of Vue Class Components: Exploring Advanced Customization Options

Currently, I am working on a project using Vue 2 with Typescript. However, I am facing an issue where I cannot add options to the component. <script lang="ts"> import { Component, Vue } from "vue-property-decorator"; import HelloW ...

The filter functionality isn't functioning properly when attempting to filter an array of objects within a Vuex action

I have a collection of objects that is accessed through a getter. I am using this getter inside an action to filter the items, but no matter what I do, the filtering does not seem to work and it ends up returning all the mapped item IDs. filterItems({ gett ...

Positioning Firefox absolutely within a table-cell element is possible

Interestingly, it's not Internet Explorer causing me trouble this time, but Firefox. Here is the Fiddle link for reference: http://jsfiddle.net/EwUnt/ The purpose of this table is to highlight both the row and column of the cell where the cursor is ...

Is it possible to utilize AJAXToolKit and Jquery on a single webpage?

Is it possible to utilize both AJAXToolKit and jQuery on a single page? For example, let's say we have ScriptManager in the same page along with including ...

Capturing a part of the screen using JavaScript for screen recording

I'm exploring the possibility of implementing a JavaScript screen recorder that captures a video feed rather than the entire screen. I'm wondering if it's achievable using getDisplayMedia or if there's a specific library for this purpos ...

How can I successfully transmit the entire event during the (change) event binding with ng-select in Angular 14?

I'm working on Front end Code <ng-select formControlName="constituencyId" placeholder="Select Constituency" (change)='onContituencyChanged($event)'> > &l ...

Is there a way to display a React component containing an array that is constantly changing due to an external function?

I am facing a challenge involving a component that needs to render an array of divs. The requirement is to add another div after a certain external function is triggered. This function must be declared outside the component for export purposes. The issue ...

techniques for tracking instance variables in a Ruby controller and transferring them to an HTML view utilizing AJAX

Hello, I am looking to create a page that dynamically monitors or renders the value of a variable within the controller as it iterates through different values. view.html.erb <a id='get_value' class="btn">Run</a> <ul id="value_va ...

The functionality of react-router-dom's NavLink isActive feature seems to be unreliable

We are currently immersed in a React.js project. I am in the process of setting up multiple pages using react-router-dom and my goal is to alter the active icon by using NavLink. The icon+sel signifies the active page. render() { const oddEvent = (mat ...

The function Event.preventDefault seems ineffective when attempting to block input of CJK (Korean) characters in a v-text-field of type

Currently, I am tackling an issue in a Vue project where I need to fix a small bug. However, the solution seems quite challenging. I have managed to make the v-text-field accept only numerical input, which is functioning well. <v-text-field type=" ...

Is it possible to verify whether a Node Js query is empty or not?

I've been attempting to determine if a result query is empty or not, conducting thorough research in the process. Below is the code illustrating the query I'm executing and the two methods I employed to check if it's empty. The issue at han ...

What is the best way to sort an array based on a person's name?

I have a list of different groups and their members: [ { "label": "Group A", "fields": [ { "value": "color1", "name": "Mike" }, { &quo ...

Creating a form with multiple checkboxes using Material-UI components where only a single checkbox can be selected

Creating a simple form using Material-UI with checkboxes to select one option and push data to the backend on submit is the goal. The Form component structure includes: multiple options represented by checkboxes only one checkbox can be selected at a time ...