From 6d023bb8797f545411003b0d7f86aa0f8edab7ca Mon Sep 17 00:00:00 2001 From: Evan You Date: Wed, 14 Jul 2021 11:34:51 -0400 Subject: [PATCH] Expose API (#343) --- active-rfcs/0042-expose-api.md | 235 +++++++++++++++++++++++++++++++++ 1 file changed, 235 insertions(+) create mode 100644 active-rfcs/0042-expose-api.md diff --git a/active-rfcs/0042-expose-api.md b/active-rfcs/0042-expose-api.md new file mode 100644 index 00000000..d66dfaac --- /dev/null +++ b/active-rfcs/0042-expose-api.md @@ -0,0 +1,235 @@ +- Start Date: 2020-09-06 +- Target Major Version: 3.x +- Reference Issues: #135, #210 + +# Summary + +Provide the ability for components to control what is publicly exposed on its component instance. This proposal unifies #135 and #210 with additional details. + +# Basic example + +## Options API + +```js +export default { + expose: ['increment'], + + data() { + return { + count: 0 + } + }, + methods: { + increment() { + this.count++ + } + } +} +``` + +## Composition API + +```javascript +import { ref } from 'vue' + +export default { + setup(props, { expose }) { + const count = ref(0) + + function increment() { + count.value++ + } + + // public + expose({ + increment + }) + + // private + return { increment, count } + } +} +``` + +Here in the both cases, other components would only be able to access the `increment` method from this component, and nothing else. + +# Motivation + +In Vue, we have a few ways to retrieve the "public instance" of a component: + +- Via template refs +- Via the `$parent` or `$root` properties + +Up until now, the concept of the "public instance" is equivalent to the `this` context inside a component. However, this creates a number of issues: + +1. A component's public and internal interface isn't always the same. A component may have internal properties that it doesn't want to expose to other components. In other cases a component may want to expose methods that are specifically meant to be used by other components. + +2. A component returning render function from `setup()` encloses all its state inside the `setup()` closure, so nothing is exposed on the public instance, and there's currently no way to selectively expose properties while using this pattern. + +3. ` +``` + +See [relevant section](https://github.com/vuejs/rfcs/blob/master/active-rfcs/0040-script-setup.md#exposing-components-public-interface) in the ` +``` + +In parent: + +```html + + + +``` + +The above does not affect the runtime API design and therefore should not block this RFC from being merged.