简体   繁体   English

我如何从<layout>在<template>到我在 Vue 中的导出默认值中的一个方法?</template></layout>

[英]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.我试图从 HTML 中的文本输入中收集文本到我的 Vue 文件的方法部分,但似乎无法弄清楚。 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.我花了几个小时研究 v-model 并弄乱它,但是,我要么做错了,要么它没有按照我的想法做。 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.我试图在单击提交按钮时将用户输入到 queryBox 中的任何内容发送到 logSomething() 方法。 Thanks in advance for any help!提前感谢您的帮助!

You can use the v-model and map your queryText variable that will contain your input field data.您可以使用 v-model 和 map 将包含您的输入字段数据的queryText变量。 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将模板中的内容传递到Express中的布局? - How do I pass content from a template to a layout in Express? Vue,如何在渲染方法中将数据传递给组件 - Vue, how do I pass data to component in render method 如何将“导出默认值”中的数据传递到导出默认值 Vue.js 之外 - How to pass data within "export default" to outside of export default Vue.js 如何从 Vue 3 中的脚本设置中导出默认值? - How do you export default from inside script setup in Vue 3? 如何从vue js模板中将params传递给方法调用? - How to pass params into method call from within vue js template? 如何使用 v-on:change 将输入文本传递给我的 vue 方法? - How do I pass input text using v-on:change to my vue method? 如何通过方法在动态创建的行中分配/传递数据 - Vue.js - How do I assign/pass data in dynamically created row through a method - Vue.js Vue.js:如何在.vue模板文件内的vue方法中设置数据变量 - Vue.js: how do you set a data variable in a vue method inside a .vue template file 如何使用`@click`将hrefs传递给Vue js中的方法? - How can I pass hrefs to my method in Vue js with `@click`? 如何从列表视图模板传递数据以使用angular来编辑视图模板 - How do I pass data from the list view template to edit view template using angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM