Unleashing the Power of v-model in Vue.js: A Comprehensive Guide to Referencing Input

I am a beginner in vue.js and I am currently facing an issue with making a get request from a field that has a v-model="embed.url" after pasting a link. The paste event works fine, but I'm struggling to reference the input with v-model="embed.url" and retrieve the data.

Every time I attempt the code below, I encounter the following errors: [Vue warn]: Error in v-on handler: "ReferenceError: embed is not defined" and ReferenceError: "embed is not defined"

This is my Vue code:

<script type="text/javascript">

axios.defaults.xsrfHeaderName = "X-CSRFToken";
new Vue({
  el: '#app',
  delimiters: ['!!', '!!'],
  data () {
    return {
      embed: {
          url: '',
          title: '',
          description: '',
          type: '',
          thumbnail_url: '',
          html: '',
      },
      isPaste: false,
      embedsinfo: [],
    }
  },
methods: {

formSubmit(e) {

    e.preventDefault();
    let currentObj = this;
    axios.post('http://wegemoc.local:8000/recipes/recipe/embed/add/', {
        url: this.url,
    })
    .then(function (response) {
        currentObj.output = response.data;
    })
    .catch(function (error) {
        currentObj.output = error;
});
},
paste() {
this.isPaste = true;
},
input() {
if (this.isPaste) {
  axios.get('http://iframe.ly/api/oembed?url=' + embed.url + '&api_key=493c9ebbdfcbdac2a10d6b')
  .then(response => (this.embedsinfo = response))
  isPaste = false;
}
  }

}, 

});

This is my form:

            <div id="app">
          !! embedsinfo.title !!
        <form method="post" class="margin-bottom-25" @submit="formSubmit">
                {% csrf_token %}

                <div class="form-group">
                  <label for="formGroupExampleInput">Recipe URL*</label>
                  <input type="url" class="form-control" placeholder="URL" @paste="paste" @input="input" v-model="embed.url">
                </div>
                <div class="form-group">
                  <label for="formGroupExampleInput2">Title</label>
                  <input class="form-control" placeholder="Title" v-model="embed.title">
                </div>
                <div class="form-group">
                    <label for="formGroupExampleInput2">Description</label>
                    <input type="textarea" class="form-control" id="formGroupExampleInput2" placeholder="Description" v-model="embed.description">
                </div>
                <div class="form-group">
                    <label for="formGroupExampleInput2">Thumbnail URL</label>
                    <input type="text" class="form-control" id="formGroupExampleInput2" placeholder="Thumbnail URL" v-model="embed.thumbnail_url">
                </div>

            <button type="submit" class="btn btn-success-gradiant">Add Link</button>
        </form>

      </div>

Answer №1

When working with Vue without Single Component files, make sure your data property is an object instead of a function. Update your data property to an object and give it a try.

new Vue({
el: '#app',
delimiters: ['!!', '!!'],
data: {
   return {
      embed: {
          url: '',
          ...
      };
},
...

Remember to use "this" when accessing properties like embed.url within your data.

if (this.isPaste) {
  axios.get('http://iframe.ly/api/oembed?url=' + this.embed.url + '&api_key=493c9ebbdfcbdac2a10d6b')
  .then(response => (this.embedsinfo = response))
  isPaste = false;
}

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

The wordpress jquery dependency is failing to respond

After converting an HTML ecommerce template into WooCommerce, I am experiencing issues with the functionality. The Nivo slider and some other product features are not working properly. It seems like they are having trouble finding WordPress jQuery, even th ...

Generating a new array and merging different sets of keys together

Is there a way to modify how items are added to a jQuery array? Here is the current code snippet in use: var sub_updated = []; $('.current-sub-items').each(function() { $(this).find('.prod-select').each(function() { if ($(th ...

Click the button to access the login form created using React components

I have been working on developing a login form that includes various input components and a button component. SignIn.js class SignIn extends Component { render() { return ( <article className="br2 ba dark-gray b--black-10 mv4 w-100 w-50-m ...

Ways to emphasize the index navigation link when on the main page

Currently, there is a web design project that I am tackling and have encountered a slight hiccup that needs resolving. The issue revolves around highlighting different navigation links based on the URL of the current page. This functionality works seamless ...

jQuery and Bootstrap collide

Here is my jQuery code that toggles visibility of different divs based on a click event: $(function () { $(".kyle-div, .tracey-div, .frank-div, .rosie-div").hide(); $("a").bind("click", function () { $(".conor-div, . ...

Adding code containing various Google Maps elements in a fresh browser window using JavaScript

I've encountered an issue while trying to create a new page with a Google map and title based on the button clicked. Interestingly, when I copy/paste the HTML in the "newhtml" variable into an actual HTML file, it works perfectly fine. However, it doe ...

The page reloads automatically following the completion of an ajax request

Hey there, I have a basic form with just a text field. When we submit the form, the data entered in the text field gets stored in the database through ajax. Although the ajax function is working properly and the data is being submitted, the page automatica ...

Reorganize the workflow in Node.js by moving the business logic from the controller to the

As I delve into learning Node.js, I've encountered a challenge with code refactoring. After researching the best practices for code architecture in Node.js, I am eager to refactor my code. The current state of my code: user.controller.js const bcry ...

Tips on updating v-for instantly?

What can be done if the product information of users can be changed by users, but the changes are not instantly reflected until the page is refreshed? <template> <div> <table> <thead> ...

Get access to the file upload field using ajax

I'm encountering an issue with accessing the file upload field //HTML <input type='file' name='images' id="images" multiple> ////////////////// $('.bt-add-com').click(function(){ var formdata = new For ...

Using a JavaScript array in Java

Currently, I am working on an Android app that requires me to download data from a JavaScript array representing the schedule for my school. The link to the data is here. I am looking for a way to parse this data into a Java array. I have considered using ...

Error: Type Error - 'style' is not defined in the Progress Bar Project

Having recently started learning Javascript, I have been engaging with various tutorials and projects in order to gain familiarity with the language. One particular tutorial that caught my attention is a Progress-Bar tutorial by "dcode" available at this l ...

Is it possible to invoke a helper function in Node.js from within a callback?

I recently started programming with node.js and I am encountering an error that I can't quite figure out. The function appears to be correctly set up, and I don't believe there are any asynchronous issues because of the self variable I have in pl ...

I'm in the process of constructing a create-next-app and I need to retrieve data from a web API. However, I'm unsure of the best place to securely store the API key

I am working on building a create-next-app that will retrieve data from the News Catcher API and display it within my application. I have obtained an API key to access the News Catcher API. However, I am unsure of where to securely store the API key and h ...

Sub-menu disappears upon resizing the screen

Currently, I am working on creating a unique responsive navigation system that transforms into a 100% width pulldown menu for mobile devices. To achieve this functionality, I have implemented some JavaScript code that hides any open sub-menu items when the ...

Text centered vertically with jQuery is only loaded after the page is resized

My jQuery script is designed to vertically center text next to an image, but it seems to only work properly after resizing the page. $(window).load(function () { $(function () { var adjustHeight = function () { $('.vertical-al ...

Ways to deactivate the Bootstrap collapse feature

In my accordion FAQs, I am facing an issue where Question 1 is automatically opened when the page loads. Additionally, when I click on Question 2, it closes instead of staying open. I would like to address these problems and make sure that each question st ...

Having trouble with Vue 3 Component State not updating following an asynchronous operation?

Encountering challenges in my Vue 3 app when trying to update a component's state post an asynchronous operation. Here's what's happening: Within a component, there is a method called containerMoveHere that utilizes Socket.io for an async o ...

Preventing FOUC when navigating away from a vue-flickity carousel/slider - the ultimate guide!

Currently, I've integrated the vue-flickity package by MetaFizzy's Flickity into my Vue application. Upon navigating away from a route containing a vue-flickity slider instance, there is a brief flash of unstyled slides visible in the document wh ...

Concurrent execution within a Node.js function

I am facing a challenge with my function that deals with 1400+ crypto pairs. Each pair requires an API call and data storage, resulting in a significant amount of time for the entire process. The bottleneck occurs because each pair takes approximately 3-4 ...