简体   繁体   中英

How to refference input in vue js with v-model?

I'm new in vue, and I am trying to make a get request from a field that has a v-model="embed.url" after pasting the link. Event after paste works well but, I don't know how to reference to input with v-model="embed.url" and get the data.

When I try code below error appear: [Vue warn]: Error in v-on handler: "ReferenceError: embed is not defined" and ReferenceError: "embed is not defined"

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;
}
  }

}, 

});

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">Adres przepisu*</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">Tytuł</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="Tthumbnail_url" v-model="embed.thumbnail_url">
                </div>

            <button type="submit" class="btn btn-success-gradiant">Dodaj link</button>
        </form>

      </div>

When you are not using Vue as Single Component files, your data property must be an object and not a function. Change your data property to an object and then give it a try.

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

Also you must access your data embed.url property using "this" like how you have referenced other properties in your code.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM