Is there a way for me to connect the ajax data to Vue components?

Utilizing Jquery ajax to load data from an external API has been successful and the v-for text is also working without any issues.

Vue

var vm = new Vue({
  el:'.inlinePoetry',
  data:{
      PoetryList:[]
  },
  created:function(){
      var self = this;
      $.ajax({
          url: "api/poetry.json",
          dataType:'json',
          type: 'GET',
          data: "data",
          cache: false,
          ifModified: true,
          success: function getData(data){
              self.PoetryList = data.PoetryList;
          },
          error: function(result){ console.log('error'); }
      })
  }

})

html

<div class="inline_frame" v-for="(item,index) in PoetryList">
    <div class="inline_content" :id="Poetryid">
        <div class="letter">
            <div class="letter-t">Title</div>
            <div>{{item.letterFirst}}</div>
            <div>{{item.letterSecond}}</div>
        </div>
        <div class="letter">
            <div class="letter-t">Description</div>
            <div>{{item.letterThird}}</div>
            <div>{{item.letterForth}} <a :href="PoetryLink">click me</a> </div>
        </div>

    </div>
</div>

However, I am encountering an issue when trying to bind the href and id in my html tags. The :id="Poetryid" and :href="PoetryLink" are using computed properties.

computed:{
  Poetryid:function(){
      // console.log(self.PoetryList.id);
      // return self.PoetryList.id;
      for(var i=0;i<PoetryList.length;i++){
          return PoetryList[i].id
      }
  },
  PoetryLink:function(){
      console.log(self.PoetryList.link);
      return self.PoetryList.link;
  }
}

I have tried using the for loop to run Poetryid, but my console shows error during evaluation. Any advice on how to resolve this would be greatly appreciated, thank you!

Answer №1

Initially, this loop will only run once and then return a value, given that PoetryList.length >= 1

// This block of code will always return PoetryList[0].id
for(var i=0;i<PoetryList.length;i++){
    return PoetryList[i].id
}

Is there a reason why you can't directly access item.id in this scenario?

<div class="inline_content" :id="item.id">

Can the same approach be taken for PoetryLink too?

<div>{{ item.letterForth }} <a :href="item.link">click me</a> </div>

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

Obtaining a value from an input field in Vue.js

Just starting out with Vue and could use a hand extracting a value from an input field: Here's what I have in my form: <input type="hidden" id="groupId" value="1"> If I were using jQuery, I would do the following: ...

Can global scope be injected into a class instantiated in ES6, also known as a singleton?

Before I get started, I want to apologize in advance for the lengthy code that is about to follow. It may make this question seem a bit bloated, but I believe it's necessary for understanding my issue. Imagine we have a predefined MainModule: ' ...

The existence of useRef.current is conditional upon its scope, and it may be null in certain

I'm currently working on drawing an image on a canvas using React and Fabric.js. Check out the demo here. In the provided demo, when you click the "Draw image" button, you may notice that the image is not immediately drawn on the canvas as expected. ...

Enhance User Experience with a Customized Progress Bar using Google Apps Script

I created a Google Sheets cell counter in Apps Script and need help setting up the bootstrap progress bar using the "percentage" variable. GS function cellCounter() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheets = ss.getSheets(); var ...

The functionality of Angular services is not available for use in Jasmine tests

As someone who is relatively new to Jasmine Testing and the Angular framework, I find myself in a unique situation. I am currently struggling with referencing my service functions in my Jasmine tests. Here is a snippet of my Angular Service Initialization ...

Utilize Javascript or Jquery to intercept and handle both GET and POST requests

Is there a method to effectively intercept and capture both GET and POST requests as they are sent from the browser to the server? In my web application, full page refreshes occur after each request is submitted, however, some pages experience delays in r ...

Obtaining data objects with Angular 2 from JSON

Recently, I received a URL that includes data arrays in JSON format. My goal is to retrieve and utilize all elements within it: However, when attempting this, I end up with everything but nothing specific. For instance: How can I access data.name or data. ...

Add fresh inline designs to a React high-order component creation

Applying a common HOC pattern like this can be quite effective. However, there are instances where you may not want a component to be wrapped, but rather just extended. This is the challenge I am facing here. Wrapper HOC const flexboxContainerStyles = { ...

Issue occurs when nested functions prevent the data() variable from updating

As a newcomer to VUE, I may not be using the right terminology so bear with me. I'm attempting to update a variable that is defined in the script tag's "data()" function. The issue arises when trying to change the value of a variable within the ...

What's the best way to invoke a function from a different JS file or create a custom event in JQuery that includes a parameter as a data object?

I am facing an issue while using requireJS to call a function from a required JS file. In my main app.js "controller", I have included (plugin)app.js, which contains all plugin configurations and related functions. The snippet below is from app.js defin ...

What is the best way to set up a local node server on a subdirectory?

In my vuejs application, I am looking to replicate a production environment locally where the deployment is on a subfolder such as www.domain.com/subfolder1. Is there a way to configure my local node server so that the application runs on localhost:port/s ...

The login process in Next-auth is currently halted on the /api/auth/providers endpoint when attempting to log in with the

My Next-auth logIn() function appears to be stuck endlessly on /api/auth/providers, as shown in this image. It seems that the async authorize(credentials) part is not being executed at all, as none of the console.log statements are working. /pages/api/au ...

An empty array is being returned from a mongoose function call

I'm currently working on a project that involves fetching random values from MongoDB using mongoose and storing them in an array. However, I am facing an issue where the array appears to be empty outside the function: exports.Run = (req, res) => { ...

ng-class: Issue detected - The symbol '-' is positioned at column {2}

I'm having an issue with ng-class. I need to add a disabled state class if the role has admin privileges, but I keep getting an error. Here is the HTML: <label class="toggle modal-label-box" ng-class="{state-disabled: checkCanModify()}"> &l ...

Issue: The object is unable to be executed as a function, resulting in the failure to return an array

Currently, I am extracting table values from the UI row by row. This involves clicking on each row and retrieving the corresponding data. exports.getTableData = function(callback){     var uiArray =[];     var count;         aLib.loadCheck(c ...

Using Firestore startAt() with Redux: a comparison of serializable and non-serializable scenarios

I find myself at a pivotal moment in my Firebase project and am seeking some general guidance. Here are the key points as I have gathered them through my research: When it comes to Firestore queries, there is a useful feature for pagination called startAt ...

Is it possible to automatically refresh an AJAX Powered Page without it reloading from the beginning?

Many websites, like Twitter and LinkedIn, use AJAX to load new content as you scroll down. Sometimes, after reaching the end of a long page loaded with AJAX (where no more items are being loaded), some content towards the top or middle gets updated. Is the ...

What is the best way to acquire the href value from this source?

Looking to extract the dynamic value "3 Sent" from the html snippet provided. How can this be achieved? <ul class="nav nav-tabs some-tabs"> <li class="active"> <a href="#accepted" data-toggle="tab">1 Accepted</ ...

Error: The document object is not defined when attempting to access it within a

<script type="module" src="/scripts/navbar.js"></script> <script> async function fetchContent(e) { e && e.preventDefault(); const response = await fetch('https: ...

The JQuery function fails to execute following a successful Ajax request

I recently ran into an issue with my Ajax call. Here's the code snippet in question: $("#start-upload-btn").click(function(){ $.ajax({ type: "post", url: "", data: { newProjectName: $('#project-name') ...