From b330e934d25ff03145ecbc923ba8324e49497197 Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 5 Jul 2021 11:55:14 -0400 Subject: [PATCH 1/3] expose api --- active-rfcs/0000-expose-api.md | 235 +++++++++++++++++++++++++++++++++ 1 file changed, 235 insertions(+) create mode 100644 active-rfcs/0000-expose-api.md diff --git a/active-rfcs/0000-expose-api.md b/active-rfcs/0000-expose-api.md new file mode 100644 index 00000000..56dfe7d2 --- /dev/null +++ b/active-rfcs/0000-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 defualt { + 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" 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, or 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. From 922e12d5da14e6b821c718731e625994eee01eed Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 6 Jul 2021 20:12:24 -0400 Subject: [PATCH 2/3] typoe fix --- active-rfcs/0000-expose-api.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/active-rfcs/0000-expose-api.md b/active-rfcs/0000-expose-api.md index 56dfe7d2..d66dfaac 100644 --- a/active-rfcs/0000-expose-api.md +++ b/active-rfcs/0000-expose-api.md @@ -11,7 +11,7 @@ Provide the ability for components to control what is publicly exposed on its co ## Options API ```js -export defualt { +export default { expose: ['increment'], data() { @@ -60,9 +60,9 @@ 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" equivalent to the `this` context inside a component. However, this creates a number of issues: +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, or a component may want to expose methods that are specifically meant to be used by other components. +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. @@ -78,7 +78,7 @@ A new option, `expose` is introduced. It expects an array of property keys to be ```js // Child.vue -export defualt { +export default { expose: ['increment'], data() { From 541b6fa5984f90b947761599b7a58cb72fadcaec Mon Sep 17 00:00:00 2001 From: Evan You Date: Wed, 14 Jul 2021 11:34:00 -0400 Subject: [PATCH 3/3] adjust rfc number --- active-rfcs/{0000-expose-api.md => 0042-expose-api.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename active-rfcs/{0000-expose-api.md => 0042-expose-api.md} (100%) diff --git a/active-rfcs/0000-expose-api.md b/active-rfcs/0042-expose-api.md similarity index 100% rename from active-rfcs/0000-expose-api.md rename to active-rfcs/0042-expose-api.md