Vue 'expose' Option


Example

Using the expose option to make a method available for the parent component to use.

export default {
  expose: ['createMessage'],
  data() {
    return {
      message: ' '
    }
  },
  methods: {
    createMessage(msg) {
      this.message += msg + ' '
    }
  }
}
Run Example »

See more examples below


Definition and Usage

The expose option is used to list which properties that are available to a parent component through template refs.

By default, all child component properties are available to a parent component through the use of template refs.

This means that if the child component has no expose option, and the parent component uses the built-in attribute ref="childComp" on the child component, the parent component can access a data property 'message' in the child component with the code this.$refs.childComp.message. (See Example 1)

But, when using the expose option, only the properties listed in the expose option are available to the parent. (See Example 2)


More Examples

Example 1

The expose option is not used in the child component, so all properties in the child component are available to the parent component.

ChildComp.vue:

<template>
  <div>
    <h3>ChildComp.vue</h3>
    <p>Message from parent component:</p>
    <p id="pEl">{{ message }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      message: ' '
    }
  },
  methods: {
    createAlert() {
      alert('This is an alert, from the child component')
    }
  }
}
</script>

<style scoped>
div {
  border: solid black 1px;
  padding: 10px;
  max-width: 350px;
  margin-top: 20px;
}
#pEl {
  background-color: lightgreen;
  font-family: 'Courier New', Courier, monospace;
}
</style>

App.vue:

<template>
  <h2>Example expose Option</h2>
  <p>The 'expose' option is not used, so all child properties are available to the parent by default, both the 'message' data property, and the 'createAlert()' method:</p>
  <button v-on:click="{ this.$refs.childComp.message += 'Hello! '; }">Write 'Hello!'</button>
  <button v-on:click="{ this.$refs.childComp.createAlert(); }">Create alert</button>
  <child-comp ref="childComp"/>
</template>
Run Example »

Example 2

Using the 'createMessage' method in the child component from the parent component does not work, because only the 'message' data property is listed in the expose option of the child component.

ChildComp.vue:

<template>
  <div>
    <h3>ChildComp.vue</h3>
    <p>Message from parent component:</p>
    <p id="pEl">{{ message }}</p>
  </div>
</template>

<script>
export default {
  expose: ['message'],
  data() {
    return {
      message: ' '
    }
  },
  methods: {
    createMessage(msg) {
      this.message += msg + ' '
    }
  }
}
</script>

<style scoped>
div {
  border: solid black 1px;
  padding: 10px;
  max-width: 350px;
  margin-top: 20px;
}
#pEl {
  background-color: lightgreen;
  font-family: 'Courier New', Courier, monospace;
}
</style>

App.vue (highlighted line does not work):

<template>
  <h2>Example expose Option</h2>
  <p>Only the 'message' data property is listed in the 'expose' option, so the 'createMessage' method from the child component is not available, and will not work:</p>
  <input type="text" v-model="inpText" placeholder="Write something">
  <button v-on:click="useMet">Use exposed method</button>
  <child-comp ref="childComp"/>
</template>

<script>
export default {
  data() {
    return {
      inpText: ''
    }
  },
  methods: {
    useMet() {
      this.$refs.childComp.createMessage(this.inpText);
    }
  },
  mounted() {
    this.$refs.childComp.message = 'Initial message!';
  }
}
</script>
Run Example »

Related Pages

Vue Tutorial: Vue Template Refs

Vue Tutorial: Vue Components

Vue Reference: Vue 'ref' Attribute

Vue Reference: Vue $refs Object


Copyright 1999-2023 by Refsnes Data. All Rights Reserved.