简体   繁体   English

组件Vuejs2声明$data obj在组件之间共享数据

[英]Component Vuejs2 declare $data obj to share data between components

I am trying to make a Vue2 component to all the select of my app so would be easier later to change it if necessary!我正在尝试为我的应用程序的所有选择制作一个 Vue2 组件,以便以后在必要时更容易更改它!

I've based my research on the example given by the docs and I am breaking my head to figure out why should I speficy all the object on the data attr to make it work!我已经根据文档给出的示例进行了研究,我正在努力弄清楚为什么我应该指定数据属性上的所有对象以使其工作!

The following code is working properly, but if we change: data: { record: { category_id: null } } by data: { record: {} } it stop to work!以下代码工作正常,但如果我们更改: data: { record: { category_id: null } } by data: { record: {} }停止工作!

Must be said the $data.record is loaded by ajax... would I always specify the whole object even knowing that after the ajax request I am going to replace all with something like this.record = response.data ?必须说 $data.record 是由 ajax 加载的...我是否总是指定整个对象,即使知道在 ajax 请求之后我将用类似this.record = response.data东西替换所有this.record = response.data

If somebody need there is FIDDLE [ https://jsfiddle.net/gustavobissolli/4xrfy54e/1/ ]如果有人需要,有 FIDDLE [ https://jsfiddle.net/gustavobissolli/4xrfy54e/1/ ]

EDIT: SORRY GUYS JUST FIXED FIDDLE LINK编辑:对不起,伙计们刚刚修复了小提琴链接

 Vue.component('select2', { props: ['options', 'value'], template: '#select2-template', data() { return { model: '' } }, mounted: function() { this.model = this.value }, watch: { value: function(value) { this.model = value }, model: function(value) { this.$emit('input', value) }, } }) var vm = new Vue({ el: '#el', template: '#demo-template', data: { record: { category_id: null }, options: [{ id: 1, text: 'Hello' }, { id: 2, text: 'World' }] } })
 <div id="el"></div> <!-- using string template here to work around HTML <option> placement restriction --> <script type="text/x-template" id="demo-template"> <div> <pre>{{ $data | json }}</pre> <select2 :options="options" v-model="record.category_id" value="record.category_id"></select2> </div> </script> <script type="text/x-template" id="select2-template"> <select v-model="model"> <option disabled>Select...</option> <option v-for="opt in options" :value="opt.id">{{ opt.text }}</option> </select> </script>

So you are trying to edit a value which didn't arrive yet?因此,您正在尝试编辑尚未到达的值? :-) :-)

The thing is: at the moment v-model="record.category_id" is "executed", you have nothing there, ie, there is no "category_id" at the "record" object.问题是:在v-model="record.category_id"被“执行”的那一刻,你什么没有,即“记录”对象没有“category_id”。 So, it binds to nothing.所以,它什么都没有绑定。 This is why the select won't work if you omit the "category_id" at data initialization.这就是为什么如果在data初始化时省略“category_id”, select将不起作用的原因。

But your assumption that when data arrives from server (ajax call) the component will not work, is wrong.但是您认为当数据从服务器(ajax 调用)到达时组件将无法工作的假设是错误的。

I have updated your fiddle: https://jsfiddle.net/4xrfy54e/4/我已经更新了你的小提琴: https : //jsfiddle.net/4xrfy54e/4/

  1. First, use the dropdown before clicking the button: since it is binded to nothing, it will not update anything.首先,在单击按钮之前使用下拉列表:由于它没有绑定,因此不会更新任何内容。 This is correct.这是对的。

  2. Now, click the button.现在,单击按钮。 The button is simulating that data arrived from the server, and is assigned to this.record of the vm.该按钮模拟从服务器到达的数据,并分配给this.record

  3. Play with the dropdown again: since record.category_id exists now, the binding is working fine.再次使用下拉列表:由于 record.category_id 现在存在,绑定工作正常。

Please, read the " Reactivity in Depth " documentation page, and you will stop breaking your head :-)请阅读“ Reactivity in Depth ”文档页面,您将不再伤脑筋:-)

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

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