]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
refactor: better type inference
authorEduardo San Martin Morote <posva13@gmail.com>
Tue, 19 Jul 2022 14:41:34 +0000 (16:41 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Tue, 19 Jul 2022 14:41:34 +0000 (16:41 +0200)
packages/playground/App.vue [deleted file]
packages/playground/src/App.vue
packages/router/src/RouterView.ts

diff --git a/packages/playground/App.vue b/packages/playground/App.vue
deleted file mode 100644 (file)
index 0501461..0000000
+++ /dev/null
@@ -1,233 +0,0 @@
-<template>
-  <div>
-    <pre>{{ currentLocation }}</pre>
-    <section class="info">
-      Name:
-      <pre id="name">{{ currentLocation.name }}</pre>
-    </section>
-
-    <section class="info">
-      Params:
-      <pre id="params">{{ currentLocation.params }}</pre>
-    </section>
-
-    <section class="info">
-      Query:
-      <pre id="query">{{ currentLocation.query }}</pre>
-    </section>
-
-    <section class="info">
-      Hash:
-      <pre id="hash">{{ currentLocation.hash }}</pre>
-    </section>
-
-    <section class="info">
-      FullPath:
-      <pre id="fullPath">{{ currentLocation.fullPath }}</pre>
-    </section>
-
-    <section class="info">
-      path:
-      <pre id="path">{{ currentLocation.path }}</pre>
-    </section>
-
-    <hr />
-
-    <label>
-      <input type="checkbox" v-model="state.cancelNextNavigation" /> Cancel Next
-      Navigation
-    </label>
-    <ul>
-      <li>
-        <router-link to="/n/%E2%82%AC">/n/%E2%82%AC</router-link>
-      </li>
-      <li>
-        <router-link :to="{ name: 'docs', params: { id: '€uro' } }"
-          >/docs/€uro (object)</router-link
-        >
-      </li>
-      <li>
-        <router-link :to="{ path: '/', query: { currency: '€uro', é: 'e' } }"
-          >/currency=€uro&é=e (object)</router-link
-        >
-      </li>
-      <li>
-        <router-link to="/documents/€">/n/€</router-link>
-      </li>
-      <li>
-        <a href="/documents/%E2%82%AC">/documents/%E2%82%AC (force reload)</a>
-      </li>
-      <li>
-        <a href="/documents/€">/documents/€ (force reload): not valid tho</a>
-      </li>
-      <li>
-        <router-link to="/home">Home (redirects)</router-link>
-      </li>
-      <li>
-        <router-link to="/">Home</router-link>
-      </li>
-      <li>
-        <AppLink to="/">AppLink Home</AppLink>
-      </li>
-      <li>
-        <router-link to="/always-redirect">/always-redirect</router-link>
-      </li>
-      <li>
-        <router-link to="/children">/children</router-link>
-      </li>
-      <li>
-        <router-link to="/children/alias">/children/alias</router-link>
-      </li>
-      <li>
-        <router-link :to="{ name: 'default-child' }"
-          >/children (child named)</router-link
-        >
-      </li>
-      <li>
-        <router-link :to="{ name: 'WithChildren' }"
-          >/children (parent named)</router-link
-        >
-      </li>
-      <li>
-        <router-link to="/children/a">/children/a</router-link>
-      </li>
-      <li>
-        <router-link to="/children/b">/children/b</router-link>
-      </li>
-      <li>
-        <router-link to="/children/b/a2">/children/b/a2</router-link>
-      </li>
-      <li>
-        <router-link to="/children/b/b2">/children/b/b2</router-link>
-      </li>
-      <li>
-        <router-link to="/nested">/nested</router-link>
-      </li>
-      <li>
-        <router-link to="/anidado">/anidado</router-link>
-      </li>
-      <li>
-        <router-link to="/long-0">/long-0</router-link>
-      </li>
-      <li>
-        <router-link to="/users/5">/users/5</router-link>
-      </li>
-      <li>
-        <router-link
-          :to="{
-            name: 'user',
-            params: { id: '' + (Number(currentLocation.params.id || 0) + 1) },
-          }"
-          >/users/{{ Number(currentLocation.params.id || 0) + 1 }}</router-link
-        >
-      </li>
-      <li>
-        <router-link to="/with-data">/with-data</router-link>
-      </li>
-      <li>
-        <router-link to="/cant-leave">/cant-leave</router-link>
-      </li>
-      <li>
-        <router-link :to="{ name: 'docs', params: { id: 'é' } }"
-          >/docs/é</router-link
-        >
-      </li>
-      <li>
-        <router-link to="/rep">/rep</router-link>
-      </li>
-      <li>
-        <router-link to="/rep/a">/rep/a</router-link>
-      </li>
-      <li>
-        <router-link to="/rep/a/b">/rep/a/b</router-link>
-      </li>
-      <li>
-        <router-link to="/parent/1">/parent/1</router-link>
-      </li>
-      <li>
-        <router-link to="/p/1">/p/1</router-link>
-      </li>
-      <li>
-        <router-link to="/parent/1/as-absolute-a"
-          >/parent/1/as-absolute-a</router-link
-        >
-      </li>
-      <li>
-        <router-link to="/p/1/as-absolute-a">/p/1/as-absolute-a</router-link>
-      </li>
-      <li>
-        <router-link to="/p_1/absolute-a">/p_1/absolute-a</router-link>
-      </li>
-    </ul>
-    <button @click="toggleViewName">Toggle view</button>
-    <Suspense>
-      <template #default>
-        <router-view :name="viewName" v-slot="{ Component, route }">
-          <transition
-            :name="route.meta.transition || 'fade'"
-            mode="out-in"
-            @before-enter="flushWaiter"
-            @before-leave="setupWaiter"
-          >
-            <keep-alive>
-              <component
-                :is="Component"
-                :key="route.name === 'repeat' ? route.path : undefined"
-              />
-            </keep-alive>
-          </transition>
-        </router-view>
-      </template>
-      <template #fallback> Loading... </template>
-    </Suspense>
-  </div>
-</template>
-
-<script>
-import { defineComponent, inject, computed, ref } from 'vue'
-import { scrollWaiter } from './scrollWaiter'
-import { useLink, useRoute } from 'vue-router'
-import AppLink from './AppLink.vue'
-
-export default defineComponent({
-  name: 'App',
-  components: { AppLink },
-  setup() {
-    const route = useRoute()
-    const state = inject('state')
-    const viewName = ref('default')
-
-    useLink({ to: '/' })
-    useLink({ to: '/documents/hello' })
-    useLink({ to: '/children' })
-
-    const currentLocation = computed(() => {
-      const { matched, ...rest } = route
-      return rest
-    })
-
-    function flushWaiter() {
-      scrollWaiter.flush()
-    }
-    function setupWaiter() {
-      scrollWaiter.add()
-    }
-
-    const nextUserLink = computed(
-      () => '/users/' + String((Number(route.value.params.id) || 0) + 1)
-    )
-
-    return {
-      currentLocation,
-      nextUserLink,
-      state,
-      flushWaiter,
-      setupWaiter,
-      viewName,
-      toggleViewName() {
-        viewName.value = viewName.value === 'default' ? 'other' : 'default'
-      },
-    }
-  },
-})
-</script>
index 050146168f726975860e9fe4d33a099ffff9e81e..b686c0c54ec28de6d446640180f954b3b7a83f1a 100644 (file)
       </li>
     </ul>
     <button @click="toggleViewName">Toggle view</button>
-    <Suspense>
-      <template #default>
-        <router-view :name="viewName" v-slot="{ Component, route }">
-          <transition
-            :name="route.meta.transition || 'fade'"
-            mode="out-in"
-            @before-enter="flushWaiter"
-            @before-leave="setupWaiter"
-          >
-            <keep-alive>
-              <component
-                :is="Component"
-                :key="route.name === 'repeat' ? route.path : undefined"
-              />
-            </keep-alive>
-          </transition>
-        </router-view>
-      </template>
-      <template #fallback> Loading... </template>
-    </Suspense>
+    <RouterView :name="viewName" v-slot="{ Component, route }">
+      <Transition
+        :name="route.meta.transition || 'fade'"
+        mode="out-in"
+        @before-enter="flushWaiter"
+        @before-leave="setupWaiter"
+      >
+        <!-- <KeepAlive> -->
+        <Suspense>
+          <template #default>
+            <component
+              :is="Component"
+              :key="route.name === 'repeat' ? route.path : route.meta.key"
+            />
+          </template>
+          <template #fallback> Loading... </template>
+        </Suspense>
+        <!-- </KeepAlive> -->
+      </Transition>
+    </RouterView>
   </div>
 </template>
 
-<script>
+<script lang="ts">
 import { defineComponent, inject, computed, ref } from 'vue'
 import { scrollWaiter } from './scrollWaiter'
 import { useLink, useRoute } from 'vue-router'
@@ -214,7 +214,7 @@ export default defineComponent({
     }
 
     const nextUserLink = computed(
-      () => '/users/' + String((Number(route.value.params.id) || 0) + 1)
+      () => '/users/' + String((Number(route.params.id) || 0) + 1)
     )
 
     return {
index 985e29fb60d30d096b036ddb10f3de5e70b46f55..d107e231a5cf23a7b81197f646b26eb800e3f2d1 100644 (file)
@@ -61,7 +61,9 @@ export const RouterViewImpl = /*#__PURE__*/ defineComponent({
     __DEV__ && warnDeprecatedUsage()
 
     const injectedRoute = inject(routerViewLocationKey)!
-    const routeToDisplay = computed(() => props.route || injectedRoute.value)
+    const routeToDisplay = computed<RouteLocationNormalizedLoaded>(
+      () => props.route || injectedRoute.value
+    )
     const injectedDepth = inject(viewDepthKey, 0)
     // The depth changes based on empty components option, which allows passthrough routes e.g. routes with children
     // that are used to reuse the `path` property
@@ -134,18 +136,19 @@ export const RouterViewImpl = /*#__PURE__*/ defineComponent({
 
     return () => {
       const route = routeToDisplay.value
-      const matchedRoute = matchedRouteRef.value
-      const ViewComponent = matchedRoute && matchedRoute.components![props.name]
       // we need the value at the time we render because when we unmount, we
       // navigated to a different location so the value is different
       const currentName = props.name
+      const matchedRoute = matchedRouteRef.value
+      const ViewComponent =
+        matchedRoute && matchedRoute.components![currentName]
 
       if (!ViewComponent) {
         return normalizeSlot(slots.default, { Component: ViewComponent, route })
       }
 
       // props from route configuration
-      const routePropsOption = matchedRoute!.props[props.name]
+      const routePropsOption = matchedRoute.props[currentName]
       const routeProps = routePropsOption
         ? routePropsOption === true
           ? route.params
@@ -157,7 +160,7 @@ export const RouterViewImpl = /*#__PURE__*/ defineComponent({
       const onVnodeUnmounted: VNodeProps['onVnodeUnmounted'] = vnode => {
         // remove the instance reference to prevent leak
         if (vnode.component!.isUnmounted) {
-          matchedRoute!.instances[currentName] = null
+          matchedRoute.instances[currentName] = null
         }
       }