简体   繁体   English

将子模型中的v模型链接到父值vue js

[英]Linking v-model in child to parent value vue js

I have a vue component which is as follows and in the child template I have a text input which I want to use v-model to link it to a var in the parent. 我有一个vue组件,如下所示,在子模板中,我有一个文本输入,我想使用v-model将其链接到父级中的var。 But its not working. 但它不起作用。 How would I be able to achieve this? 我怎么能做到这一点?

I'm currently using Vue.js 1.23 (needing to use it for certain reasons so upgrading isn't an option) 我目前正在使用Vue.js 1.23(由于某些原因需要使用它,所以升级不是一个选项)

Thank you 谢谢

// this is in my Vue app
textWidget: ''

// this is called directly in the page.html
<text-widget v-show="getCurrentWidgetType() == 'text-widget'" :textWidget="textWidget"></text-widget>

// declaring the component just before my main vue app
Vue.component('text-widget', {
    template: `{% include('templates/widgets/text.html.twig') %}`,
    date: function () {
        return {
            textWidget:textWidget
        }
    }
})

Update : 更新:

// declared just before my Vue app
Vue.component('text-widget', {
    template: `{% include('templates/widgets/text.html.twig') %}`,
    props: ['textwidgetmodel']
})

// text input in the component child
<input type="text" v-model="textwidgetmodel">

// html in the main html page
// this input was just for testing. This confirmed that the props binding did indeed work, 
// the only problem is it seems to be a one way binding from parent to child. 
// I assume this because when I change the value with the text input below then it changes the value even in the component element. 
// But when a value in inputted into the child text input nothing happens to the value in the partent
<input type="text" v-model="textWidget" />

<text-widget v-show="getCurrentWidgetType() == 'text-widget'" :textwidgetmodel=textWidget></text-widget>

Okay, so I guess there is somewhere the input element that has model binded to the textWidget , something like this: 好的,所以我猜有一个输入元素的模型绑定到textWidget ,如下所示:

<input type="text" v-model="textWidget"/>

And you want to send this to your child component via props, so you did It like this 并且你想通过道具将它发送给你的孩子组件,所以你这样做了

<text-widget v-show="getCurrentWidgetType() == 'text-widget'" :textWidget="textWidget"></text-widget>

This is almost good, but not enough because props in template should be formatted in the kebab-case, so this is correct setup 这几乎是好的,但还不够,因为模板中的道具应该在烤肉盒中格式化,所以这是正确的设置

<text-widget v-show="getCurrentWidgetType() == 'text-widget'" :text-widget="textWidget"></text-widget>

And then you should define that prop into the component logic, so Vue can know what to use (you don't need data model here now, except you have some another component based data): 然后你应该将prop定义到组件逻辑中,这样Vue就可以知道要使用什么了(你现在不需要数据模型,除了你有另一个基于组件的数据):

// declaring the component just before my main vue app
Vue.component('text-widget', {
    template: `{% include('templates/widgets/text.html.twig') %}`,
    props: ['textWidget']
})

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

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