Passing props to route.push in Vue with TypeScript does not work as expected

In my application, I have a straightforward structure consisting of a table and a page for creating a new item. To navigate to the create page and pass parameters, I utilize a button on the main page.

initNewRow(): void {
    let someData: string = 'someText';
    this.$router.push({
        name: 'catalog-create___en',
        params: { someData: someData }
    });
}

The goal is to pass certain parameters during navigation without having a dedicated route/page that needs to be displayed in the menu. Instead, there should only be a page with a table (without a specific page for item creation).

{
    id: 8,
    label: 'menuitems.Catalog.text',
    link: '/catalog/main',
    icon: 'ri-eye-line',      
    meta: {
        middleware: ['router-auth']
    }
},

Upon opening the page for creating an item, I encounter issues with the props not being available or properly initialized.

@Component({
    name: 'Create',
    props: ['someData']
})
export default class Catalog extends Vue {
    @Prop({ required: true })
    someData!: any;

    constructor() {
         super();
         this.someData = this.$route.params.someData;
         console.log(this.someData);
    }

I'm currently facing an error stating that I should avoid mutating a prop directly, but rather use data or computed properties based on the prop's value. The specific error message reads: "Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "someData""

Answer №1

As stated on the official Vue documentation

These are the correct ways to navigate, the method you are currently using will not work

const userId = '123'
this.$router.push({ name: 'user', params: { userId } }) // -> /user/123
this.$router.push({ path: `/user/${userId}` }) // -> /user/123
// This will NOT work
this.$router.push({ path: '/user', params: { userId } }) // -> /user

Try updating it to something like

this.$router.push({ name: 'create', params: { someData } });

You can view the available routes in your project using Vue devtools: https://i.sstatic.net/uqCDD.png

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

Angular ensures that the fixed display element matches the size of its neighboring sibling

I have a unique challenge where I want to fix a div to the bottom of the screen, but its width should always match the content it scrolls past. Visualize the scenario in this image: https://i.sstatic.net/i7eZT.png The issue arises when setting the div&apo ...

Exploring the integration of Styled-components in NextJs13 for server-side rendering

ERROR MESSAGE: The server encountered an error. The specific error message is: TypeError: createContext only works in Client Components. To resolve this issue, add the "use client" directive at the top of the file. More information can be found here i ...

Attributes for 'v-bind' directives must have a value specified

Struggling to implement a tree structure in vue.js and encountering an issue with element props. Any assistance would be greatly appreciated. I've attempted using :content="{{tempCont}}" as well as content="{{tempCont}}", but neither approach proved ...

Module for importing text without verifying types using wildcards

Here is a unique module definition using the wildcard character: declare module 'custom!*' { const data: string; export default data; } Check out how it's imported: import * as myData from 'custom!./myCustomData.txt'; B ...

Obtain the range of values for a match from an array using Vue.js

I have an array in my Vue.js code with values 25, 100, 250, and 500. I am looking to find the key value that matches a specific range. For example, if the input is 5, then the output should be 25. However, the code I tried returns all values within the ran ...

Switch over to TypeScript - combining Socket.IO, Angular, and Node.js

This is the code I'm using for my node server: import http from 'http'; import Debug from 'debug'; import socketio, { Server } from 'socket.io'; import app from './app'; import ServerGlobal from './serve ...

Using Angular2 in conjunction with the simpleheat plugin

I've been attempting to integrate the NPM plugin Simpleheat () into my Angular2 app, but unfortunately, the heatmap is not rendering even though everything appears to be set up correctly. You can find the full repository with the issue here: https:// ...

String Compression - Number of Elements

Suppose I define a specific type: type SomeType = 'a' | 'b' | 'c' Is there a TypeScript function available that can calculate the number of unique values a variable of type SomeType can hold? assertEq(countUniqueValues(SomeTy ...

When using Ionic Vue, the `this.$router.push('/home')` command successfully changes the link, but unfortunately it continues to

After logging in, I am trying to redirect to the home page. Using the following code: this.$router.push() The URL changes from localhost:8100/auth/login to localhost:8100/home However, the page remains the same, i.e., the Login Page. The routes ind ...

What is the reason that the ES6 module import expression fails to import JSON files located in the assets folder when using certain path arguments?

A puzzling ES6 import scenario came up where the following import statement did not function as expected within methods of a Vue.js SFC: const fullContentFile = '@/assets/rules/rules.json' import(fullContentFile).then(data => console.log(&apos ...

Testing a Vue.js component using inline templates for unit tests

Currently, I am utilizing Vue 2 to improve the functionality of a Ruby on Rails engine. The incorporation of inline-template attributes in the existing Haml views serves as templates for my Vue components. I have encountered an issue while attempting to t ...

Providing a description of the type in a way that permits the use of one key while restricting the usage of another key

Apologies for my limited English skills, could you kindly explain how to specify a type where one key can be entered only if another specific key is also filled? interface Base { top?: number; left?: number; behavior?: "auto" | "smooth ...

Babel exclusively processes JavaScript files in my Vue project, rather than the project as a whole

I need to make my Vue project compatible with an old iPad running iOS/safari version 5, which requires transpiling it to ES5 using Babel. Here is the content of my babel.config.js: presets: [ //'@vue/cli-plugin-babel/preset', ["@babel/ ...

What's the reason for switching from a custom tab icon to a React icon?

I've encountered a strange issue while working on my app - the tab icon keeps changing from the one I've set to the ReactJS icon whenever I navigate between different routes. I have linked the icon correctly in my HTML file, so I'm not sure ...

Receiving the error "Potential null object. TS2531" while working with a form input field

I am currently working on developing a straightforward form to collect email and password details from new users signing up on Firebase. I am utilizing React with Typescript, and encountering an error labeled "Object is possibly 'null'. TS2531" s ...

Implementing route middleware within the layout of NUXT 3: Is it possible?

For a while now, I've been contemplating the idea of incorporating Nuxt middleware into a layout. While unsure if it's feasible, my experience with Nuxt 2 leaves me hopeful that it could be achievable in Nuxt 3. In this particular project, there ...

The recognition of Angular ngrx union type actions is hindered by discrepancies in constructors

The actions classes and union type are displayed below. Unfortunately, these actions are not being recognized during the application's execution. export class Login implements Action { readonly type = LOGIN; constructor( public payload: { ...

Inertiajs - Seamlessly transition to a different page within the same environment

Is there a way to navigate between pages on the client side in Laravel and InertiaJS without using inertia.visit()? I am curious about how to switch pages solely within the client, without having to communicate with the server. Can props be specified fro ...

Comparing dates in Angular and Ionic: A breakdown

I am facing an issue with comparing dates in my ion-datetime input and a date variable obtained from the server. The problem arises because when I retrieve the value from the input, it includes the time field along with the date. As a result, using the get ...

Tabulator: Adding a new row with merged columns in Tabulator

Is there a method to insert a new row in Tabulator that spans multiple columns? view image here I am looking for something similar to this, where data is displayed across more than one column. I need to incorporate additional details retrieved from an aj ...