Passing an object in Vue.js during a redirect

i currently have two components named projectListComponent and projectSingleComponent. I want to pass an object from component 1 to component 2 during redirection. Here is my code:

projectListComponent.vue

<template>
 <div class="row justify-content-center">
        <div class="col-sm-6 col-md-4" v-for="(item, index) in projects" :key="item.id">
            <div class="card" v-if="percentCompletedTask(index) != '100%'">
                <div class="card-body">
                    <h5 class="card-title">{{item.title}}</h5>
                    <h6 class="card-subtitle mb-2 text-muted">
                    <div class="progress">
                        <div class="progress-bar" role="progressbar" :style="{width:percentCompletedTask(index)}" aria-valuemin="0" aria-valuemax="100">{{percentCompletedTask(index)}}</div>
                    </div>
                    </h6>
                    <p class="card-text">{{item.description}}</p>
                    <router-link :to="{ name: 'projectSingleComponent', params: {id: item.id, project: item} }">Read More</router-link>
                </div>
            </div>
        </div>
    </div>
</template
<script>
import projectSingleComponent from '@/components/projectSingleComponent.vue'

    export default {  
        name: 'projectListComponent',     
        data() {
            return {
                components: {projectSingleComponent},
                newProject:
                    {
                        title: '',
                        description: '',
                        status: false,
                        task:[]
                    },
                projects:[
                    {
                        id: 1,
                        title: 'ptyxiakh',
                        description: 'TEI',
                        status: false,
                        task:[{title:'task1', completed: false}, {title:'task2', completed: true},{title:'task2', completed: true}]
                    },
                    {
                        id: 2,
                        title: 'vue app',
                        description: 'My first vue js app',
                        status: false,
                        task:[{title:'task1', completed: false}]
                    },
                    {
                        id: 3,
                        title: 'Codeigniter',
                        description: 'My first codeigniter app',
                        status: false,
                        task:[{title:'task1', completed: false}, {title:'task2', completed: true}]
                    },
                    {
                        id: 4,
                        title: 'Codeigniter_new',
                        description: 'My first codeigniter app',
                        status: false,
                        task:[{title:'task1', completed: true}, {title:'task2', completed: true}]
                    },
                    {
                        id: 5,
                        title: 'nexctloud',
                        description: 'My first codeigniter app',
                        status: false,
                        task:[{title:'task1', completed: true}, {title:'task2', completed: true},{title:'task2', completed: true}]
                    },
                    
                ],
                completedProject:[],
                uncompletedProject:[]
            }
        },
        
        }
    }
    

</script>

projectSingleComponent.vue

<template>
    <div>
        <h2>Id: {{project.id}}</h2>
        <h2>title: {{project.title}}</h2>
        <h2>description: {{project.description}}</h2>
        <h2>status: {{project.status}}</h2>


       
    </div>
</template>

<script>
    export default {
        name: 'projectSingleComponent',  
        data() {
            return {
                project: {
                    id: 0,
                    title: '',
                    description: '',
                    status: false,
                    tasks: []
                }
                
            }

        },
        created() {
            let projectData = this.$route.params;
            alert(projectData)
            
            
        }  
    };
</script>

my application displays the message "[object Object]" when redirected. How can I properly parse the data from the array projects (in projectListComponent) to the second component?

Answer №1

When you trigger a notification or display an object in JS, it automatically attempts to convert it into a string which is why you see "[object Object]" printed. It may not be very informative, but you can still alert(projectData.id) or alert(projectData.project) to see the specific parameters that were originally passed.

Answer №2

For a more efficient way to work with objects, consider using console.log(projectData) instead of relying on alert. This method can make it simpler to navigate and understand how to access object properties.

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 procedure for utilizing the comparator to arrange items according to various attributes?

I am trying to find a way to arrange my models in a collection based on their required flag and then alphabetically by their value. This is what my current code looks like: var myModel = Backbone.Model.extend({ defaults: { required: true, ...

Updating an href based on the presence of a variable in the URL

I've implemented an AMP layout where a unique code is added to the URL based on the traffic source. This code is used to update phone numbers displayed on the site. For instance, if you visit https://example.com/text?bid=1234 I created a script that ...

Generate a JSON structure from HTML with the help of jQuery

Issue Overview Imagine a scenario where there is a delivery of assorted candies. The delivery consists of multiple boxes, each containing various types of unique candies. Each box and candy type has its own distinct identifier. Moreover, every candy comes ...

"Encountering an issue with TestCafe's Selector - usage of the .find()

I've been having difficulty defining a selector in TestCafe using the ".find" method. My goal is to click on the link "Start a claim" as shown in the image below: https://i.sstatic.net/2BIRK.png Even though I'm using the id element and trying to ...

Angular displaying a blank screen, even though the complete dataset is available

I am currently working on my first website using Angular and I've encountered a problem. When I click on 'view project', it should return the data specific to that item. The strange thing is, when I log my JavaScript console, I can see all t ...

Encountering a "Module not found" issue while attempting to utilize the Google APIs Node.js client

Attempting to incorporate the Google API Node.js client into a fresh React Web application. Here is my package.json: { "name": "onboarding", "version": "0.1.0", "private": true, "devDependencies": { "react-scripts": "0.8.4" }, "dependencie ...

Vue JS - Issue with rendering <tr> and <td> elements in table component

I have created a custom table component using vue.js: <template> <div> <table class="table border"> <thead> <tr> <td>header 1&l ...

What are the steps for retrieving data on the server side by utilizing a token stored in localStorage?

Currently, I am diving into the official documentation for Next.js and utilizing the App router. On the data fetching patterns page, it explicitly states: Whenever possible, we recommend fetching data on the server To adhere to this recommendation, I cr ...

Display the HTML content retrieved from the SailsJS Controller

Exploring the world of SailsJS, I am on a mission to save HTML content in a database, retrieve it, and display it as rendered HTML. To achieve this goal, I have set up a sails model and a controller. This is what my model looks like: attributes: { ht ...

Puppeteer Alert: Unable to Locate Node for specified selector

When using Puppeteer to interact with an input element on a requested URL, I encountered an issue. First, I entered a quantity like this: await page.type('#bidamount_temp', bidAmount); However, when trying to click on the following button after ...

What techniques can I use to merge alpha channels with compositing in images?

Utilizing an HTML5 Canvas along with the KineticJS(KonvaJS) canvas library, I have successfully incorporated an Image element. My current objective is to erase specific pixels while maintaining a certain level of transparency. The red dot erases pixels at ...

The Angular UI Datepicker is not reflecting changes in scope variables within the dates-disabled function

I'm currently working with AngularJS and the Angular UI Bootstrap. I've encountered an issue that I initially thought was related to scope, then initialization, but now I'm stuck trying to figure out what's causing the problem. I' ...

AngularJS ng-repeat - cascading dropdown not refreshing

I'm dealing with an AngularJS issue where I'm trying to create a cascade dropdown like the one below: <div class="col-sm-2 pr10"> <select class="PropertyType" ng-controller="LOV" ng-init="InitLov(140)" ng-model=" ...

Maximizing efficiency when retrieving information from JSON files

Is there a way to optimize data retrieval for faster loading and determining data length without loading the entire JSON data? Check out this code snippet: const url = "https://jsonplaceholder.typicode.com/comments"; const [data, setData] = useState([] ...

Dynamically loading components in Vue Router depending on the route parameter

I am currently in the process of developing an editor application that can accommodate multiple design templates. Each template comes with its own unique set of fields, and therefore has a designated .vue file. My goal is to dynamically load the appropria ...

Setting the y-axis range in d3.js and nvd3.js: A complete guide

I'm attempting to define the y-axis range of the chart to be between 1 and 100. After reviewing the API documentation, I came across a potential solution involving axis.tickValues which can be found here: https://github.com/mbostock/d3/wiki/SVG-Axes# ...

Ways to implement a parallax effect by changing images within a specific div on scrolling with the mouse

First and foremost, thank you for taking the time to review my question. I am currently developing a website and I need to implement a vertical image transition effect within a selected division, similar to a parallax effect. Within this div, there are 3 ...

Having trouble making the swing effect trigger on mouseover but not on page load

I am trying to achieve a swinging effect on mouseover only, not on page load. Below is the JS code I have: (function swing() { var ang = 20, dAng = 10, ddAng = .5, dir = 1, box = document.getElementById("box"); (function setAng(ang){ ...

unleashing the magic of AJAX: a guide to extracting

In my Symfony project, I am attempting to retrieve the content of an AJAX request in order to check the data using dump(). The purpose is to process this data and perform a SQL query. However, when I use dump() in my controller, there doesn't appear t ...

Triggering a modal window with unique content by clicking on various images

Is there a way to trigger a modal window through clicking on an image? Additionally, can different images open different content upon clicking? I am planning to showcase a portfolio where clicking on an image will activate a modal that displays other image ...