]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
feat(sfc): useAttrs + useSlots
authorEvan You <yyx990803@gmail.com>
Wed, 23 Jun 2021 01:00:26 +0000 (21:00 -0400)
committerEvan You <yyx990803@gmail.com>
Wed, 23 Jun 2021 01:07:42 +0000 (21:07 -0400)
Deprecate useContext

packages/runtime-core/src/apiSetupHelpers.ts
packages/runtime-core/src/component.ts

index 03955277a0da04f862891f0bc565b8a715a16ffc..21c4d68f684f440c8d642d24b30df6f836687da3 100644 (file)
@@ -61,10 +61,27 @@ export function defineEmits() {
  */
 export const defineEmit = defineEmits
 
+/**
+ * @deprecated use `useSlots` and `useAttrs` instead.
+ */
 export function useContext(): SetupContext {
+  if (__DEV__) {
+    warn(
+      `\`useContext()\` has been deprecated and will be removed in the ` +
+        `next minor release. Use \`useSlots()\` and \`useAttrs()\` instead.`
+    )
+  }
   const i = getCurrentInstance()!
   if (__DEV__ && !i) {
     warn(`useContext() called without active instance.`)
   }
   return i.setupContext || (i.setupContext = createSetupContext(i))
 }
+
+export function useSlots(): SetupContext['slots'] {
+  return useContext().slots
+}
+
+export function useAttrs(): SetupContext['attrs'] {
+  return useContext().attrs
+}
index d15fbbab1a473d740feb137e4596f0a6881b6e03..5eabaa178bc835c2810c927fd06b665998f0a677 100644 (file)
@@ -815,11 +815,9 @@ export function finishComponentSetup(
   }
 }
 
-const attrHandlers: ProxyHandler<Data> = {
+const attrDevProxyHandlers: ProxyHandler<Data> = {
   get: (target, key: string) => {
-    if (__DEV__) {
-      markAttrsAccessed()
-    }
+    markAttrsAccessed()
     return target[key]
   },
   set: () => {
@@ -847,7 +845,7 @@ export function createSetupContext(
     // properties (overwrites should not be done in prod)
     return Object.freeze({
       get attrs() {
-        return new Proxy(instance.attrs, attrHandlers)
+        return new Proxy(instance.attrs, attrDevProxyHandlers)
       },
       get slots() {
         return shallowReadonly(instance.slots)