简体   繁体   中英

How do I pass data from <layout> in <template> to a method in my export default in Vue?

I'm trying to collect the text from a text input in HTML to the methods section of my Vue file but can't seem to figure it out. I've spent a couple hours researching v-model and messing around with it but, I was either doing it wrong or it doesn't do what I thought it did. Here's my code:

 <template> <div id="app"> <Layout> <input id="queryBox" placeholder="Enter your SQL query here." size="60"/> <br> <input type="submit" v-on:click="logSomething()"> </Layout> </div> </template> <script> export default { data: () => { return { queryText: "" } }, methods: { logSomething() { console.log(this.queryBox) } } } </script>

I'm trying to send whatever the user inputs into queryBox to the logSomething() method upon the click of the submit button. Thanks in advance for any help!

You can use the v-model and map your queryText variable that will contain your input field data. And then on click, use the event and make the submit not to load form, just to avoid load page.

<template>
  <div id="app">
    <Layout>
      <input id="queryBox" v-model="queryText" placeholder="Enter your SQL query here." size="60"/>
      <br>
      <input type="submit" v-on:click="logSomething">
    </Layout>
  </div>
</template>

<script>
  export default {
    data: () => {
      return {
        queryText: ""
      }
    },
    methods: {
      logSomething(e) {
        e.preventDefault()
        console.log(this.queryText)
      }
    }
  }
</script>

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