简体   繁体   English

如何从Vue组件获取输入数据

[英]How to get input data from Vue component

I searched many internet sites but didn't find useful snippets for my problem resolution.我搜索了许多 Internet 站点,但没有找到用于解决问题的有用片段。
I wanted to achieve simple data passing from input field value in Vue component to Vue root element with a method for data fetching,我想通过一种数据获取方法实现从 Vue 组件中的输入字段值到 Vue 根元素的简单数据传递,
eg, simply when I press submit button, the browser alerts me with a msg message instead of undefined .例如,当我按下提交按钮时,浏览器会用msg消息而不是undefined来提醒我。
With Vue technology, I absolutely don't understand how to do this.使用Vue技术,我完全不明白如何做到这一点。 I spend almost a day trying to understand this.我花了将近一天的时间试图理解这一点。 Actually, I don't get how Vue even works with these roots and components even after reading basics nth times.实际上,即使在阅读了第 n 次基础知识之后,我也不明白 Vue 是如何处理这些根和组件的。 Is there any simple explanation?有什么简单的解释吗?

Please help!请帮忙!

    <div id="form">
    <button v-on:click="submitBtn">Save</button>
    <custom-form>
    </custom-form>
</div>
<script>

    var testApp = Vue.createApp ({
        methods: {
            submitBtn: function(){
                alert(submit.test)
            }
        }
    })
    
    testComponent.component('custom-form', {
        template: `
            <label>test<input type="text" v-model=value :value=this.test></label>
        ` ,
        data: function() {
            return {
                test: 'msg'
            }
        },
        methods: {
            test: function() {
                return this.test
            }
        }
    })
    
    const vm = testApp.mount('#form')

</script>

simply when I press submit button, the browser alerts me with a msg message instead of undefined .只是当我按下提交按钮时,浏览器会用msg消息而不是undefined来提醒我。

Here is an example I've just tested that I think will help you:这是我刚刚测试过的一个示例,我认为它会对您有所帮助:

In app.js file:在 app.js 文件中:

Vue.createApp({
  data() {
      return {
        test: ''
      };
  },
  methods: {
      testMessage() {
          return alert(this.test);
      }
  }
}).mount('#form');

And in index.html file:在 index.html 文件中:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vue.js</title>
</head>
<body>
        <div id="form">
            <label for="testInput">Test Message</label>
            <input type="text" id="testInput" v-model="test" />
            <button v-on:click="testMessage">Submit</button>
        </div> 
        
    <script src="https://unpkg.com/vue@next"></script>
    <script src="app.js"></script>
</body>
</html>

在此处输入图片说明

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

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