Observing a nested object and all its properties in Vue

I am trying to monitor a nested object in my code. Here is the snippet:

watch: {
        'input.source.location': {
            handler: () => {
                console.log("locations");
            }
        },
        'input': {
            handler: () => {
                console.log("all the rest");
            },
            deep: true
        }
    },

If I modify the location property, I only want "locations" to be displayed. How can I achieve this?

Appreciate any guidance.

Answer №1

While I can't guarantee that this is the exact solution, it may be worth a try. Vue might not have any specific arguments that directly address your situation without utilizing an if statement.

watch: {
        'input.source.location': {
            handler: () => {
                console.log("locations");
            }
        },
        'input': {
            handler: (newVal) => {
                if(newVal.source.location) return;
                console.log("all the rest");
            },
            deep: true
        }
    },

-OR-

watch: {
        'input': {
            handler: (newVal) => {
                if(newVal.source.location) console.log("locations");
                else console.log("all the rest");
            },
            deep: true
        }
    },

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 best method for checking if a template has successfully rendered in an Iron:Router route during a mocha test?

I am working on testing if a specific template has rendered in my meteor app for a particular route. My current setup involves iron:router, practicalmeteor:mocha, and I'm using Blaze for rendering. There are a couple of challenges that I am facing: ...

Angular's $q.defer() function will yield an object with a "then" function

In my Angular file, I am attempting to access a database using $http and then store the retrieved data in a $scope variable for display on the webpage. However, I am encountering difficulties with $q.defer not running as expected. When I check the consol ...

Finding the index of an object key within a JSON structure

I am attempting to retrieve the index of my selected value in order to display all deeper options. The structure of my data in JSON format is as follows: { "flow": { "startmessage": "Hello [name]", "questionmessage": "What do you have a questi ...

Create a Nuxt component with styling and webpack to display an image sourced from the

I am utilizing styled components to create buttons: import styled from 'vue-styled-components'; const buttonProps = { color: String, br: String, pad: String, bgc: String, bgch: String, icon: String, }; export default styled('bu ...

The response from Ajax in JavaScript may come back as undefined

I'm facing an issue with my JavaScript function that uses AJAX to call a PHP function inside a PHP class. The problem is that the console.log shows undefined. function SpinTimeTotal(){ $.ajax({ type:"POST", url: &qu ...

Using a string to access a property within a ReactJS object

I am looking to simplify my React Component by referencing a JS object property using a string. This will allow me to remove repetitive conditional statements from my current render function: render() { const { USD, GBP, EUR } = this.props.bpi; ...

Unable to post form attribute value after submission

I have created a form that looks like this: <form method="POST" action="create.php" data-id="0" class="postForm"> <input type="hidden" id="#formId" value="1"> <textarea class="formBodyText"></textarea> <button typ ...

Attempting to send a request from the front-end to the back-end is resulting in a 404 endpoint error

I encountered an issue while sending a post request from the front end to the backend. The error message I received was: " Error: Request failed with status code 404 " " Had Issues POSTing to the backend, endpoint " My main concern is ...

Is the size of the array significant in the context of JavaScript here?

Whenever a button is clicked on the page, I am dynamically creating an array in javascript using item id's fetched from the database. Each entry in the array will hold a custom object. The id's retrieved from the database can range from numbers ...

Tips for passing a parameter (such as an ID) through a URL using ng-click to display a subdocument belonging to a particular user in

I am looking to retrieve specific user subdocument data on a separate page by passing the id parameter in a URL using ng-click in AngularJS. <tr ng-repeat="register in registerlist | filter:searchText"> <td>{{$index+1}}</td> <td&g ...

Is there a way to incorporate a fade-in effect when I trigger the expand function in this script?

I recently came across a jQuery plugin for expanding and collapsing content. I am interested in adding a fade-in effect to this plugin specifically when the EXPAND button is clicked. How can I accomplish this? $(document).ready(function () { var maxlines ...

Here is a way to attach a function to dynamically generated HTML that is returned in an AJAX request

As I was working on a new function development project, I encountered a situation where I had to add a function to dynamically generated HTML through an ajax call. The following is a snippet of the code: $.ajax( 'success': function(data){ ...

Getting json data through ajax in asp.net

I am facing an issue with the variable data in the function ShowFavorites as it is showing as undefined even though my ajax call is returning a json string. <script type="text/javascript"> $(document).ready(function () { ShowFavorites(); fu ...

Is there a way to automatically retrieve CSV data using ashx on a web page?

After researching the provided links from SO without success, I decided to reach out here for help. (For privacy reasons, the actual URL and header data have been obscured) I am struggling to automate downloading data from an HTTPS web page using Delphi ...

Having trouble loading environment variables in NextJS on Heroku?

I am currently utilizing NextJS and deploying my application on Heroku. When the page initially loads, I am able to retrieve data through getInitialProps without any issues. However, when trying to access this data in a regular function, I encounter an er ...

Utilizing Windows Azure and restify for node.js Development

I have an azure website with a URL like: . In my server.js file, I have the following code: var restify = require('restify'); function respond(req, res, next) { res.send('hello ' + req.params.name); next(); } var server = restify ...

MUI: Interaction with a button inside a MenuItem when not interacted with MenuItem itself?

Currently, I am utilizing MUI's Menu / MenuItem to create a menu of missions / tasks similar to the screenshot below: https://i.sstatic.net/6FGrx.png The MenuItem is interactive: // ... other code <MenuItem value={mission.issu ...

Creating an HTML SELECT element without a default blank option in the dropdown list using Angular

Issue: I am encountering a problem with setting a blank item to appear in the dropdown list of my form in Angular and JavaScript. As someone who is new to both languages, I have not been able to find a solution yet. Currently, my code looks like this: $ ...

jQuery can be used to obtain the label for a checkbox with a particular value

Currently, I am facing an issue with retrieving the label for a checkbox using jQuery. Let me provide you with the relevant HTML code: <div class="checkbox"> <label><input type="checkbox" name="cb_type[]" value="sold" >Sold</label ...

Highlight the active class on the Angular Navbar

I have been successfully using [routerLinkActive]="['active']" in my application to add an active class when the button on navbar is clicked and redirects to example.com/home. However, I noticed that if I only navigate to example.com, the active ...