简体   繁体   English

如何将输入值数据从 Vue 组件传递到根元素

[英]How to pass input value data from Vue component to root element

I want to post json data from Vue to php, but I'm struggling to find a way to pass input value data from Vue component to root element.我想将 json 数据从 Vue 发布到 php,但我正在努力寻找一种将输入值数据从 Vue 组件传递到根元素的方法。 When I call method submitProduct, alert message simply gives me 'undefined'.当我调用方法 submitProduct 时,警报消息只会给我“未定义”。 I had to strip my original code because of that stupid post balance policy.由于那个愚蠢的后余额政策,我不得不剥离我的原始代码。 What's wrong here?这里有什么问题?

  var productForm = Vue.createApp ({
            methods:{
                submitProduct: function(){
                    alert(JSON.stringify(productForm.product))
                    this.postData(productForm.product)
                },
                postData: function(p){
                    fetch('mysql_postdata.php', {
                        method: 'POST', 
                        mode: "same-origin",
                        credentials: "same-origin",
                        headers: {
                            "Content-Type": "application/json"
                        },
                        body: JSON.stringify({p:p})
                        //body: 'd='+d
                    })
                }
            }
        })
        
        productForm.component('custom-form', {
            props: ["value"],
            template: `
                <label>SKU<input type="text" :value=this.product.sku></label>
                <label>Name<input type="text" :value=this.product.name></label>
                <label>Price<input type="text" :value=this.product.price></label>
            ` ,
            
            data: function() {
                return {
                    product: {
                        sku: 0,
                        name: 'asdf',
                        price: 0
                    },
                    options: ['Size', 'Weight', 'Dimensions'],
                    selected: 'Size'
                }
            }
        })
        
        productForm.component('status-bar', {
            props: ['info'],
            template: '<p>{{ selected }}</p>'
        })
        
        const vm = productForm.mount('#product_form')

The product state belongs to the custom-form component so the root app cannot access the state. product状态属于custom-form组件,因此根应用程序无法访问该状态。

If you trying to create a form and get the input from the form, you need to do 1 of this:如果您尝试创建表单并从表单中获取输入,则需要执行以下操作之一:

  1. Lift the state to the root and pass down the custom-form and bind an event to listen to the state change docs here .将状态提升到根并传递custom-form并绑定一个事件以在此处收听状态更改文档 (only do this if the custom-form component is not deep down in the component tree) (仅当custom-form组件不在组件树的深处时才这样做)
  2. Using the state management store like Vuex to share the state within the app (in case the child component is deep down in the tree you have to pass the state down so many levels, using store management will solve that).使用像 Vuex 这样的状态管理存储在应用程序中共享状态(如果子组件在树的深处,您必须将状态向下传递这么多级别,使用存储管理可以解决这个问题)。 docs here .文档在这里 If your app is really small consider the provide/inject API.如果您的应用程序非常小,请考虑提供/注入 API。
  3. Another choice is using the provide/inject API (similar to the context provider in react).另一种选择是使用提供/注入 API (类似于 react 中的上下文提供者)。

First of all after 3.5 days of struggling to try to understand Vue I came with tested successfull result.首先,经过 3.5 天的努力尝试理解 Vue,我得到了测试成功的结果。 I want to thank you and anybody else, who helped me to understand basics principles in Vue!我要感谢你和其他任何人,他们帮助我了解了 Vue 的基本原理! :) Please see link below... :) 请参阅下面的链接...

https://jsfiddle.net/e2mnh4xa/ https://jsfiddle.net/e2mnh4xa/

PS And yes! PS 是的! You are right about rearranging 'custom-form' tag.您对重新排列“自定义表单”标签是正确的。 :) :)

html code: html代码:

<div id="product_form" v-cloak>
   <custom-form>
                
   </custom-form>
</div>

js code: js代码:

var productForm = Vue.createApp ({})
            
productForm.component('custom-form', {
  props: {
    modelValue: {
      type: String,
      default: ''
    },
  },   
  components: ['status-bar'],
  template: `
    <button v-on:click="submitProduct">Save</button>
    <label>SKU<input type="text" v-model="this.product.sku"></label>
    <label>Name<input type="text" v-model="this.product.name"></label>
    <label>Price<input type="text" v-model="this.product.price"></label>
` ,

  data: function() {
    return {
      product: {
        sku: 0,
        name: 'asdf',
        price: 0,
      },
      options: ['Size', 'Weight', 'Dimensions'],
      selected: 'Size',
      outputData: ''
    }
  },
  computed:{
    model:{
      get(){ return this.modelValue },
      set(v){ this.$emit('update:modelValue',v)}
    }
  },

  methods:{
    submitProduct: function(){
      alert (this.showData());
      return this.postData(this.product)
    },
    showData: function(){
      console.log(this.product.sku)
      return JSON.stringify(this.product)
    }
  }
})

const vm = productForm.mount('#product_form')

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

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