What causes data to update in a Vue template but not in the JavaScript portion of the code?

The topic in the template section updates when its value in the parent component changes, however, the same value in the script part remains the initial value it received at

search_params.set('param1', this.topic);
. Everything else in the code is functioning correctly. You can test it out here http://jsfiddle.net/6gk13sep/1/

If you're curious about the expected output, pressing the other button should prompt the app to re-request the API with that button's name

 Vue.component('child', {
  template: `
  <div>
  breedKey: {{ breedKey }}
  <br />
  topic: {{ this.topic }}
  <br />
  API DATA TEST: {{ this.point0 }}
  </div>
  `,
  props: ["breedKey", "time"],
  computed: {
    topic() {
      return this.breedKey;
    }
  },

  data() {
    return {
      point0: [],
      point1: [],
      point2: [],
    };
  },

  async created() {

    try {

      var url = new URL('https://www.mustavi.com/TimeSeries/?param1=China&param2=00');
      var search_params = url.searchParams;

      // new value of param set to my topic
      search_params.set('param1', this.topic);

Answer №1

Make sure to call the API within the watch method instead of just in created. This way, updates to your props will trigger the API call again.

Check out the documentation on how to use watchers for guidance on implementing this with your breed prop.

watch: {
  async breedKey (oldVal, newVal) {
     // Put your API call code here
  }
}

I've made changes to your JSFiddle demo to demonstrate the correct implementation.

Answer №2

To resolve the issue, simply include v-if="breedKey" in the child component like so:

<child v-if="breedKey" v-bind:breed-key="breedKey" v-bind:time="time"> </child>

The reason behind this solution is that when the "created" hook of the child component is triggered, the value of the topic (breedKey) from the parent component has not been retrieved yet. By using the v-if directive, the child component initializes lazily until the "breedKey" receives its value from the parent component.

Check out an example here: http://jsfiddle.net/yangjunjun/z49wak5v/1/

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

Creating a fresh JSON structure by utilizing an established one

I have a JSON data that contains sections and rubrics, but I only need the items for a new listing. The new object named 'items' should consist of an array of all the items. The final JSON output should be sorted by the attribute 'name&apos ...

Can you please provide instructions on how to expand the view in the title bar for a JavaScript/CSS/HTML UPW project?

Struggling to integrate the title bar in a UWP project using JavaScript/CSS/HTML and having trouble accessing the necessary APIs. Came across a custom helper class written in c++ in UWP samples on Github, but unable to reference it in my javascript code. H ...

Unable to retrieve props from server-side page to client-side component in a Next.js application router

Currently, I am utilizing app router alongside Next.js version 13.5. Within my /dashboard page (which is a server component), there is an ApiKeyOptions client component embedded. However, when attempting to pass props from the dashboard page to the ApiKeyO ...

Dynamic Font Formatting in Rails Table Based on Conditions

I need to customize the font color of dynamic values based on the state_id. If incident.state_id = 1, the font should be red; if incident.state_id = 2, it should be yellow; and if incident.state_id = 3, it should be green. incident.state_id = 1 : state.na ...

What is the process for integrating GitHub repository code into my client-side JavaScript application?

I am attempting to incorporate the GitHub repository "zipcelx" into my client-side JavaScript, but all I see is an option to download it from npm, which I do not understand. It would be helpful if someone could explain how a module meant for client-side us ...

AngularJS Component enthusiasts

While going through a tutorial on the Angular UI Router GitHub page (link: https://github.com/angular-ui/ui-router), I came across an intriguing code snippet: var myApp = angular.module('myApp', ['ui.router']); // For Component users, ...

Enforcing character limits in summernote

Is there a way to set a character limit on Summernote? I've tried setting maxlength on the textarea without success. Check out the Summernote GitHub page $("#textareaid").summernote({ toolbar:[ ['style', ['style']], ...

Troubleshooting Issue with Angular Property Binding for Image Dimensions

Currently, I am exploring property binding in Angular through an example. The idea is to use an image and bind its width, height, and src attributes using property binding. Surprisingly, even though there are no errors and the image renders successfully vi ...

Using ngrx to automatically update data upon subscription

Background The technology stack I am using for my application includes Angular 4.x, ngrx 4.x, and rxjs 5.4.x. Data is retrieved from a websocket as well as a RESTful API in order to share it between multiple components through ngrx. Currently, data is ref ...

Utilize Nuxt 3 dynamic components to trigger API calls using route parameters

I am in the process of developing a Nuxt 3 application for educational purposes that involves utilizing dynamic routes to fetch data from an API upon page load. My main focus is on leveraging the route parameter id with the composition API to retrieve exte ...

While continuing to input text, make sure to highlight a specific element from the search results list

Currently, I am using a customized search feature in my React.js application. I am looking for a way to focus on an element within the search results so that I can navigate using arrow keys. At the same time, I need to keep the input field focused to conti ...

When does the ng-disable function become activated?

Here's an example: <button ng-disabled="!isSomethingValid() || loading || disabled" ... class="btn btn-primary"> What determines the condition for the ng-disable attribute to evaluate its expression? ...

What could be causing my post request to function properly in POSTMAN but not in my React application?

Here are my POSTMAN headers along with the settings I used to send my POST. It only started working when I switched the Content-Type to application/json. https://i.stack.imgur.com/Xz2As.png https://i.stack.imgur.com/aJtbD.png This pertains to the server ...

Loading WordPress posts using Ajax

Struggling with getting an Ajax post loader to work properly. Following the jQuery code from a previous StackOverflow post titled "Load More Posts" with Ajax in WordPress, but still facing issues. Trying to implement an isotope list for ajax loading more p ...

Utilizing jQuery to convert object properties into a table

Here is the table structure I am working with: <table> <thead> <tr> <th>Loan Type</th> <th>Amount Borrowed</th> <th>Current Payment< ...

What is the process for including an "everything" alternative on a dropdown menu?

Need assistance with my dropdown component that filters a list based on 'state' data. Below is the HTML code for the dropdown: <section class="select-wrapper {{wrapperClass}}" [ngClass]="{'expanded': toggle}" (click)="toggleSelect($ ...

Changing the texture of a specific GLTF model in Three.js that is being replicated multiple times

Currently, I am loading a GLTF asset and using it in the scene multiple times. I am trying to modify the material color of all meshes inside a specific object that is a GLTF model. traverseMaterials(object, (material) => { material.color.set( ...

Utilizing VueJS to display content conditionally with v-show and JSON Data

I'm in the process of creating a VueJs portfolio where I want to showcase projects based on selected tags. Below is my code: <script lang="ts"> import projectsData from "../projects.json" export default { data() ...

How can I prevent Chrome from constantly prompting for permission to access the camera?

I have been working on some HTML/JavaScript/WebRTC projects that involve using the webcam. Despite hosting the files from my local web server (127.0.0.1), Chrome always prompts me for permission to access the camera each time I reload the page. Is there a ...

Using percentages to position Div elements

Currently, I am working on an HTML page that requires a div element spanning the full width of the page. My goal is to arrange other divs within this full-width div using percentages rather than pixels. The class associated with this div is .question. Thi ...