material-components-vue icon indicating copy to clipboard operation
material-components-vue copied to clipboard

implement event based registration as mixin

Open matsp opened this issue 7 years ago • 2 comments

Avoid direct child access via $children.

Parent components should only listen for specific IDs.

e.g.

  • FormField <-> Checkbox

matsp avatar May 25 '18 05:05 matsp

I'm planning on adding a function emit in baseComponentMixin like the following

function emit (target: Element, eventType: string, payload) { /* ... */ }

and change the mounted hook in every component

<div @initialized="onInit"></div> <!-- some material component here -->
mounted () {
    this.mdcComponent = MDCComponent.attachTo(this.$el) // initialize itself and may cause some DOM and style change
    this.$nextTick(() => { // use nextTick because we have to wait for the DOM change  
        this.emit(this.mdcComponent.someChild_.root_, 'initialized', this.mdcComponent.someChild_) // useful for components that initialize their children like drawer, text-field and tab bar
        this.emit(this.$slots.default.$el, 'form-initialized', this.mdcFormField) // useful for form field
    })
},
methods: {
    onInit (component) {
         this.mdcComponent = component 
    },
    onFormInit (formFieldComponent) { /* ... */ }
}

tychenjiajun avatar Oct 18 '19 08:10 tychenjiajun

Use provide/inject together with event is also a solution

tychenjiajun avatar Oct 19 '19 03:10 tychenjiajun