記錄vue認識過程中1.0到2.0不同
- v-el v-ref >> ref
2.0中v-el和v-ref被廢除稠肘,ref屬性代替
ref is used to register a reference to an element or a child component.The reference will be registered under the parent component’s $refs object. If used on a plain DOM element, the reference will be that element; if used on a child component, the reference will be component instance:
<!-- vm.$refs.p will be the DOM node -->
<p ref="p">hello</p>
<!-- vm.$refs.child will be the child comp instance -->
<child-comp ref="child"></child-comp>
this.$refs.p
this.$refs.child
- 2.0中去掉了events實例屬性和$dispatch,$broadcast事件綁定
- 1.0 組件通信方式
子組件
父組件this.$dispatch('eventName', 'hello')
event:{ 'eventName':function(msg) { console.log(msg) } }
- 2.0中可以通過一個空vue對象實現(xiàn)一個事件處理器
main.vue
子組件new Vue({ el: '#main', data () { return { eventsHub: new Vue() } }, router: router, render: h => h(App) })
父組件this.$root.eventsHub.$emit('eventName','hello')
https://vuejs.org/v2/guide/migration.html#dispatch-and-broadcast-replacedcreated () { this.$root.eventsHub.$on('eventName',(msg) => { console.log(msg) }) }
- 1.0 組件通信方式