Refresh the content of VueJS Resource

Resource file helper/json_new.json

{
  "content": {
    "content_body": "<a href='#' v-on:click.prevent='getLink'>{{ button }}</a>",
    "content_nav": "",
  }
}

Vue script.js file

new Vue({
    el: 'body',

    data: {
        text: 'Lorem sss',
    },

    methods: {
        getLink: function(){
            this.$http.get('http://localhost/vuejs/helper/json_new.json').then((resp) => {

                this.$set('text', resp.data.content.content_body);

            }, (resp) => {
                console.log('error');
            })
        }
    }
})

Output: Not Displayed Properly

<a href="#" v-on:click.prevent="getLink">{{ button }}</a>

Issue: The click event is not triggering. Data cannot be retrieved.

Answer №1

Vue.resourse does not have any impact on this issue since the HTML string from JSON is not compiled.

Here is a small test based on your example:

<body>
    <a href='#' v-on:click.prevent='getLink' v-text="button"></a>
    <div v-el:sample></div>
</body>
var test = new Vue({
  el: 'body',

  data: {
    button: 'Lorem sss',
  },

  methods: {
    getLink: function(){
      var r = Math.floor(Math.random() * (4 - 1)) + 1;
      this.$set('button', ['','btn1','btn2','btn3'][r] );
    },

    getCompiled: function() {
      $(this.$els.sample).empty()
      var element = $(this.$els.sample).append("<a href='#' v-on:click.prevent='getLink'>{{ button }}</a>");
      this.$compile(element.get(0));
      $(this.$els.sample).prepend('<p>Compiled button:</p>')
    }
  },

  ready: function() {
    this.getCompiled();
  }
})

jsfidle

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

Move two markers on Google Maps simultaneously

After researching various articles and posts on Stack Overflow about selecting multiple markers in Google Maps, I have been unable to find a solution for dragging two markers simultaneously. While I've managed to make it work so that one marker can tr ...

Generating an interactive Datepicker using Jquery

How can I design a dynamic date picker similar to the image provided below? I have attempted to create one, but I am looking for a more interactive date picker. Is there a way to achieve the design shown in the image? The current date picker does not meet ...

What is the best method to calculate the total of multiple input values from various cells and display it in the final cell of an Angular table?

Hey there! I have a challenge where I need to calculate the sum of input values for each cell and display it dynamically in the last cell of the row. Take a look at the image below: https://i.stack.imgur.com/0iKEE.png In the image, you can see that the nu ...

Tips for selectively executing a script based on whether the DIV is within the viewport

I have a created a script as shown below: jQuery(function($) { $('.count').countTo({ from: 0, to: '400', speed: '3000', refreshInterval: 50, onComplete: func ...

React Native vector icons display enigmatic symbols

I recently installed react-native-vector, but I'm seeing strange symbols when using it. Can anyone provide guidance on how to properly utilize this library? Platform: Android import React from 'react'; import {View, Text, StyleSheet} from & ...

Encountering the error message "Uncaught TypeError: Cannot read property 'addEventListener' of null with querySelector

I attempted using getElementsByClassName but encountered the same error, which is peculiar. I had to change <p id="trigger-overlay"> in my HTML to <p class="trigger-overlay"> error function toggleOverlay(){alert('fire');}; var tri ...

I am interested in scraping a webpage, how should I go about it?

Similar Questions: How to create a web crawler? Top techniques for parsing HTML I've always been curious about accomplishing a task like this. Although I do not own or manage the website (), the information I am interested in is publicly acce ...

What methods can I implement to showcase random images in JavaScript using a JSON array value?

I am currently working on a fun avatar generator project. The challenge I'm facing is that each hairstyle consists of two parts (front and back), and when loaded randomly, the colors don't always match. To tackle this issue, I have organized the ...

Can you guide me on implementing an onclick event using HTML, CSS, and JavaScript?

I am looking for a way to change the CSS codes of the blog1popup class when the image is clicked. I already know how to do this using hover, but I need help making it happen on click instead. The element I want to use as the button; <div class=&quo ...

The beforeEach hook in Mocha.js does not support the bail(false) functionality

Whenever I attempt to initiate my mocha test with the instruction bail(false), I am looking to ensure that the tests do not halt even if an error is encountered in a beforeEach hook. Despite setting this configuration, it seems like it's not working ...

Refresh the PartialView only after clicking submit

I am struggling with updating only the contents of my partial view after clicking an input type="submit button. My goal is to refresh just the partial view and update it with the updated model from the controller code, without refreshing the entire page. S ...

show button after the page has finished loading

I have a button similar to this: <input type="submit" id="product_197_submit_button" class="wpsc_buy_button" name="Buy" value="Add To Cart"> However, I am encountering an issue where if the user clicks the button before all scripts are loaded, an e ...

Range slider position not being updated after user interaction

I am working on a webpage with an embedded video and a range slider that serves as a progress bar. Using Javascript, I update the value of the range slider as the video plays, allowing users to navigate through the video content. However, I have encounter ...

Use jQuery to switch back and forth between the login and registration forms on a single

I've set up two forms, one for login and one for registration, with the default view showing the login form. There's a link that says "Don't have an account?" and when it's clicked, the registration form will display while the login for ...

Retrieve a nested JSON item using Java code

I'm working with a JSON object that has the following structure: { "accessToken" : "<dont need this>", "clientToken" : "<nor this>", "selectedProfile" : { "id" : "<nope>", "name" : "<I need this>", ...

The proper method for organizing a nested array object - an obstacle arises when attempting to sort the array

I have a collection of data fetched from Web API 2.2 stored in an Angular array as objects. Each object represents a Client and includes properties like name, surname, and a collection of contracts assigned to that client. Here is the interface definition ...

Using JavaScript to execute a JSON parse function will not work

I am currently working on this code and would appreciate any assistance. I'm trying to retrieve and parse data from my listApp.json file to display a list with one link. As a beginner, I could use some guidance. <script type = "text/javascript"> ...

Validating Names in Vue.js

I need to create a form input field for a person's name. How can I make sure that only alphabets are allowed and numbers are not permitted? <div> <input type="text" class="from-input" placeholder="Enter Your Name..." v-model="name" /> < ...

The error in React syntax doesn't appear to be visible in CodePen

Currently, I am diving into the React Tutorial for creating a tic-tac-toe game. If you'd like to take a look at my code on Codepen, feel free to click here. Upon reviewing my code, I noticed a bug at line 21 where there is a missing comma after ' ...

Validation of default values in contact forms

Obtained a free contact form online and hoping it works flawlessly, but struggling with validation for fields like "Full Name" in JS and PHP. Here is the HTML code: <input type="text" name="contactname" id="contactname" class="required" role="inpu ...