Using Vue to extract data from objects and assign them to the 'this' keyword

Is there a way to utilize object destructuring in my Vue component data? Here is my current code:

data: () => ({
  descricao: '',
  perfilBase: null,
  anotherField: '',
  otherOne: false
}),

mounted () {
  this.descricao = this.$route.query.descricao
  this.perfilBase = this.$route.query.perfilBase
}

Could I achieve the same result using object destructuring in the mounted hook like this?

mounted () {
  { this.descricao, this.perfilBase } = ...this.$route.query
}

Answer №1

If you want to utilize destructuring with targets that are not simple variables, you will have to explicitly specify the name of the destructured property instead of using shorthand syntax. For example:

mounted () {
  ({ descricao: this.descricao, perfilBase: this.perfilBase } = this.$route.query);
}

Alternatively, if the query object only contains these two properties, using Object.assign is a more straightforward approach:

mounted () {
  Object.assign(this, this.$route.query);
}

Answer №2

It seems challenging to be as concise as you desire.

The most effective approach may be

mounted () {
   const { description, baseProfile } = this.$route.query
   this.description = description
   this.baseProfile = baseProfile
}

Answer №3

Can someone test this out? I'm currently away from my computer.

{ x, y } = this.$route;
[ this.x, this.y ] = [x, y];

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

After closing the bootstrap vue alert, it refuses to function properly

I've been experimenting with creating a global alert component using bootstrap vue alert. I'm utilizing the vuex store to manage the alert's state. Below, you can find my Alert.vue component: <template> <b-alert show :variant="v ...

What is the optimal approach for managing script initialization on both desktop and mobile devices?

I have implemented a feature on my website that can detect whether the viewer is using a mobile device. Additionally, I have created a JavaScript script that adjusts settings based on whether the user is on a mobile device or not. However, I am wondering ...

Using setTimeout is necessary to make scrollIntoView work in React

My application features a simple input where users can enter a message. This message is then displayed at the bottom of all other messages, resembling a chat interface. When a new message is added to the array, I want the page to automatically scroll down ...

Performing asynchronous tasks within an indexedDB cursor

I am utilizing the indexedDB Promised library to convert the indexedDB API into promises. It seems that once my fetch operation is completed, my indexed db transaction becomes inactive. Could it be timing out? The error message I am encountering is: DOM ...

Sorting a targeted section of an already organized 2D Array based on updated values, solely if the initial sorted values align in Javascript

A challenging scenario I need help with involves an array containing objects, each with a score and a rank as shown below: [ { "score": 20, "rank": 12 }, { "score": 20, "rank": 7 }, { "score": 34, "rank": 4 } ] To begin w ...

Vue is tuned in to the input of the enter key

Recently, I created a form component called CreateDocument within my Nuxt project. Along with this component, I also implemented an autocomplete feature known as AutoCompleteFilters. However, I encountered a problem where hitting the enter key inside the ...

What is the process for extracting an array from an object?

How can I retrieve an object from an array using Node.js? I have tried the code below, but it's returning the entire object. What I actually need is to only print the name, for example, just "sethu". Code: var sethu = [{ name:'sethu', ...

Implementing a software system combining VUE and Flask technology for application deployment

My current project utilizes Flask as the backend and VUE as the frontend. To ensure everything functions properly, I find myself having to run both my Flask instance and VUE instance simultaneously. Is this standard practice in software development? As a ...

The event listener fails to function properly in asynchronous JavaScript

The reason for the error is due to the asynchronous nature of the code, where the button is not loaded yet. Is there a solution to this issue? It's difficult to explain in words but essentially, when the window is loaded, the button is not present as ...

What is the best way to extract values from a JavaScript function?

As someone who is new to Javascript, I am interested in learning how to retrieve values from a function. In the given code snippet, my goal is to extract TheName, TheHeight, TheGender, and TheSexuality when executing the function so that I can utilize the ...

Is there an equivalent to Tomcat specifically designed for Node.js applications?

Looking for an application server that offers an administration interface to deploy node.js applications, access log files, and manage running applications with options to start, stop, restart, and monitor? ...

An operator in rxjs that transforms an Observable of lists containing type X into an Observable of type X

Currently, I am facing a challenge while dealing with an API that is not very user-friendly using Angular HTTP and rxjs. My goal is to retrieve an Observable<List<Object>> from my service. Here's a snippet of the JSON output obtained from ...

The combination of Webpack with vue-loader and postcss-modules leads to an absence of values in the $

Within my application, I am initializing a Vue app that utilizes single file .vue components. To bundle, I rely on Webpack and utilize vue-loader + postcss-modules to generate scoped classes. However, I am facing an issue where I cannot access the gener ...

Placing a cookie using nookies within the Next.js API directory

I'm currently facing an issue while trying to use the nookies npm package to set a cookie within the Next.js api folder. I've successfully set up a cookie using the same code with nookies before, but for some reason, it's not working in this ...

SharePoint 2013 only allows the first AJAX request to work successfully, except when in Edit Mode

I am facing a challenge of making two AJAX requests simultaneously on a SharePoint page. One request originates from the current site, while the other comes from a team site. When I use different URLs (/events) and (/travel), each one works fine when use ...

Using Jquery Ajax to fetch a request and display it within a bootstrap carousel

My current challenge involves pulling ads into a bootstrap carousel on a chrome extension. Initially, I encountered an error message that read: bootstrap.min.js:6 Uncaught TypeError: Cannot read property 'offsetWidth' of undefined After some ...

Dispatching actions to update the Redux state is not reflecting any changes

Currently, I am developing a feedback board website and implementing a roadmap route where users can track website improvements. On mobile, I want to incorporate a three-section slide feature: planned, in-progress, and live stages. I found a useful code sn ...

Utilize the Stripe Payment Gateway within your Cordova/Phonegap Application

I have spent a considerable amount of time searching for a solution. I am looking to incorporate Stripe Payment Gateway into my cordova application. Is there a method to achieve this on both Android and iOS using JavaScript? ...

a guide on adding multiple variables into an array in a React Native application

I have a list of 6 variables with corresponding values that I need to push into the "socialMediaDetail" array in a specific format. Can someone please help me with this example? Thank you. // List of 6 variables const Twittername = 'twitter'; co ...

Ajax Syntax Error: Unexpected Token U

I have been struggling all day with an issue while trying to send json data via ajax to Express. Here is how my ajax code looks like: $('#saveClause').click(function () { var username = document.getElementById('postUserName').inne ...