[英]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:如果您尝试创建表单并从表单中获取输入,则需要执行以下操作之一:
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
组件不在组件树的深处时才这样做)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.