Guide to utilizing the "deep method" within vue.js

I have a vue component that I am working with.
I want to be able to use the watch feature for a specific keyword instead of everything.
To achieve this, I created a computed function to focus on it.
The code snippet below demonstrates how I accomplished this successfully.

var vm = new Vue({
  el: '#app',
  computed: {
    foo() {
      return this.item.foo;
    }
  },
  watch: {
    foo() {
      console.log('Foo Changed!');
    }
  },
  data: {
    item: {
      foo: 'foo'
    }
  }
})

However, when I implemented this in a different example, it did not work as expected. I believe I need to include 'deep: true' within the 'changedOptions' section of the watch property.
But I am unsure how to utilize 'deep' within a function. Can you suggest any potential solutions?

data(){
   return {
       deliveryOptions: {
           weight: 3,
           options: { express: false, shuttle: false, bike: true, walk: false },
   },

 computed: {changedOptions() {
             return this.deliveryOptions.options;
            }
 },

 watch: {
    changedOptions(){
        console.log('changed')
    }
 }

Answer №1

Computed values are only executed when they are utilized somewhere in the code.

You can incorporate computed values either within the template section or within the script.

Let's demonstrate how to use it within the mounted lifecycle hook below:

mounted () {
  console.log(this.changedOptions)
  // This will trigger the computed value to be calculated and returned.
}

If the watcher is still not running as expected, you can try setting 'immediate: true' in the watcher function like this:

 watch: {
    changedOptions: {
       immediate: true, 
       handler () {
          console.log('changed')
        }
    }
 }

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

How to create a PHP MySQL weekly calendar with pagination for Monday to Friday events in 7 days?

Looking for the best approach to create a "7 day calendar" in the form of an HTML table with columns for "Name, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday". Each row in the table will display data from a database, including the pers ...

What is the process for inserting a generated value into a graph?

Hello there! I'm new to the world of web design and might not have all the technical terms down just yet. My current project involves creating a graph that displays the average rating for each lecture. I've already calculated the averages for all ...

Fatal error: artisan storage:link in Laravel terminated

the non-functional link and the generated file content I have established a directory called "storage" within the public folder and attempted to execute: artisan storage:link however, it results in the message "Killed". I am looking to store images in ...

Trouble with Firebase/Firestore documentation queries in JavaScript

Having trouble using the new Firestore system. I'm trying to retrieve a Collection of all users and go through it, but I can't seem to make it work. db.collection("users").get().then(function(querySnapshot){ console.log(querySnapshot.dat ...

Add an item to an array and then transform the array into a JSON format

I have a situation where I am creating an object and pushing it into an array. After that, I convert it into JSON format. When I display the dataCharts using an alert, it is returned in this form: [{"AllLinks":"Link9","LinkURL":"url1"},{"AllLinks":"Link6" ...

Tips for effectively deploying my Vue application? (CLI3)

Here it says that npm run build should generate a dist folder and an index.html file. However, in my case, the index.html file is located within the dist folder. I upload this folder to my remote server, but when I view the index.html in my browser, all I ...

Exploring MongoDB querying with two keys in conjunction with Express JS and NodeJS

I'm seeking assistance with my web application developed using Express JS and Node. I am encountering issues with the .find() command when trying to search for multiple words. It's interesting because using the $all command works when two words ...

Tips for utilizing ConfirmButtonExtender selectively in asp.net?

Working on a .net web application project and utilizing ConfirmButtonExtender for confirmation. My goal is to validate all required fields first, then display a confirm message to the user conditionally. Here is the code snippet: <asp:Button ID="Updat ...

How to send a JSON object using Node.js Express 4.0

I'm currently working on parsing the JSON object sent in the request and displaying the data being transmitted. Below is my post request: $.ajax({ url: url, type: 'POST', contentType: 'application/json' ...

A JavaScript code snippet to format a phone number in the pattern xx-xxxxxx

Please help me create a JavaScript function that can determine if the text in a textbox is a number. If it's not a number, I would like the function to focus on the textbox and change the format to look like this (xx-xxxxxx) when numbers are typed in, ...

Guide to resolving Vue 2 and UIKit autocomplete template issues

I am encountering an issue with the Vue 2 + UIKit autocomplete feature. The template for the UIKit autocomplete is shown in the following code: <script type="text/autocomplete" v-pre> <ul class="uk-nav uk-nav-autocomplete uk-autocomplete-res ...

Data sent through AJAX messaging is not being acknowledged

I recently made an AJAX request and set it up like this: $.ajax({ data : { id : 25 }, dataType : 'json', contentType : 'application/json; charset=utf-8', type : 'POST', // the rest of the ...

Track the scrolling of <div> using pixel values

Is there a way to create a script that can show and hide a span based on the scrolling position of a div? $("span").hide(); $(".box").scroll(function() { if (/* <div> scolling is euqal or less than 10px away from bottom: */){ $("span").show( ...

Error: The JSON input unexpectedly ended, however the PHP file itself is error-free

When trying to display data retrieved using PHP through JSON/Ajax, I encountered an error message: [object Object] | parsererror | SyntaxError: Unexpected end of JSON input The PHP script is functional (I can view the JSON output by directly accessing th ...

Function returns to execute another Function

Update: Is there a way to retrieve a value from a nested function and have it returned by the parent function? function returningFn() { function otherFn() { var value = 123; return value; //To be retrieved by returningFn } } I nee ...

What is the best way to determine a value by analyzing the items in a refined angularjs repeater?

I'm in the process of creating a scoring system and need to calculate a total score based on values from a different set of data. I came across this reference: Calculating sum of repeated elements in AngularJS ng-repeat, but I haven't been succes ...

Executing a single Function within the UseEffect Hook

Can anyone assist me with solving this code puzzle? I have a carousel element that includes icons for moving to the previous and next slides. Whenever these icons are clicked, a specific function needs to be triggered within the useEffect() hook. The spec ...

Dynamic form in Ant Design React for calculating total

In my Ant Design dynamic form, I have the capability to input both price and quantity in various rows. I can easily add new rows to the form. Upon submission, I was able to retrieve all the values from the rows using the following code snippet: const o ...

Asset in Laravel Vue Vite is being rejected

While working with Laravel Inertia and Vite on my local machine, everything runs smoothly. However, I have encountered a problem that has left me stumped. Whenever I try to access my application from another device, I run into an issue where two JS files ...

What is the most effective way to display child elements in a React component without explicitly passing them

I created a component: const PapeConainer = ({children}) => { return ( <div //some classes and other stuff> {children} </div> ); } However, I received a comment on my merge request stating that I don't need to pass chi ...