Skip to content

Make directives available to all components within a render tree #6732

Closed
@trainiac

Description

@trainiac

What problem does this feature solve?

If you have a directive that relies on a piece of user data (e.g. language), in server side rendering you need to be able to create a new directive per request.

const getTranslateDirective = lang => ( {
  bind () {
    // access to lang
  },
  update () {
    // access to lang
  }
})

const translate = getTranslateDirective(() => parsedLangHeader.language)

// PROBLEM - this sets translate a directive globally across reqeusts
Vue.directive('translate', translate)

However this is a problem because when you call Vue.directive('translate', translate) this changes the translate directive across ALL requests.

What does the proposed API look like?

What is needed is a way to make directives available to a component and all of it's children components

const getTranslateDirective = lang => ( {
  bind () {
    // access to lang
  },
  update () {
    // access to lang
  }
})

const translate = getTranslateDirective(() => parsedLangHeader.language)

new Vue({
  directives: {
    translate: {
      module: translate,
      provide: true // Makes the directive available to this component and all of it's children.
    }
  }
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions