2

I would like to pass provide and inject to a "page-section" component that could contain itself recursively using a slot.

The goal is for each one of these "page-section" components to pass its id concatenated after the rest of the passed down ids (separated by /) to create a path of all the containing components.

Some of the these "page-section" could also contain other types of components.

This is what I was trying unsuccessfully:

 Vue.component('text-input', {
      props: ['text', 'id'],
      template: '<input type="text" :value="text"',
      inject: ['sectionPath']
    });

    Vue.component('page-section', {
      props: ['id'],
      template: '<div><slot></slot></div>',
      inject: {
        sectionPath: { default: '/' }
      },
      provide: {
        sectionPath: this.sectionPath + '/' + this.id
      }
    }
  });

1 Answer 1

1

Instead of inject at the beginning, you can define as prop.

And you need to use function for provide to use context i believe.

Vue.component('text-input', {
  props: ['text', 'id'],
  template: '<input type="text" :value="text" />',
  inject: ['sectionPath']
});

Vue.component('page-section', {
  template: '<div><slot></slot></div>',
  props: {
    id: {
      type: String,
      required: true
    }
    sectionPath: {
      type: String,
      required: true,
      default: '/'
    }
  }
  provide () {
    return {
      sectionPath: this.sectionPath + '/' + this.id
    }
  }
});

Sign up to request clarification or add additional context in comments.

1 Comment

Using the function form of provide fixed my issue. thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.