@@ -2,7 +2,8 @@ import prefetch from './prefetch'
22import { canPrefetch , supportIntersectionObserver , inBrowser } from './utils'
33
44function installRouterPrefetch (
5- Vue ,
5+ /** @type {import('vue').App } */
6+ app ,
67 { componentName = 'RouterLink' , prefetch : enablePrefetch = true } = { }
78) {
89 const observer =
@@ -29,18 +30,18 @@ function installRouterPrefetch(
2930 } , timeout )
3031 }
3132
32- const RouterLink = Vue . component ( 'RouterLink' ) || Vue . component ( 'router-link' )
33+ const RouterLink = app . component ( 'RouterLink' ) || app . component ( 'router-link' )
3334
3435 if ( process . env . NODE_ENV === 'development' && ! RouterLink ) {
3536 console . error (
36- `[vue-router-prefetch] You need to call Vue .use(VueRouter) before this plugin!`
37+ `[vue-router-prefetch] You need to call app .use(VueRouter) before this plugin!`
3738 )
3839 }
3940
4041 const Link = {
4142 name : componentName ,
42- extends : RouterLink ,
4343 props : {
44+ ...RouterLink . props ,
4445 prefetch : {
4546 type : Boolean ,
4647 default : enablePrefetch
@@ -53,12 +54,15 @@ function installRouterPrefetch(
5354 default : 2000
5455 }
5556 } ,
57+ setup ( props , context ) {
58+ return RouterLink . setup ( props , context )
59+ } ,
5660 mounted ( ) {
5761 if ( this . prefetch && observer && canPrefetch ) {
5862 requestIdleCallback ( this . observe , { timeout : this . timeout } )
5963 }
6064 } ,
61- beforeDestroy ( ) {
65+ beforeUnmount ( ) {
6266 this . unobserve ( )
6367 } ,
6468 methods : {
@@ -72,21 +76,28 @@ function installRouterPrefetch(
7276 observer . unobserve ( this . $el )
7377 }
7478 } ,
75- getComponents ( ) {
76- return this . $router . getMatchedComponents ( this . to ) . filter ( Component => {
77- return Component . cid === undefined && typeof Component === 'function'
78- } )
79+ getRouteComponents ( route ) {
80+ return route . matched
81+ . map ( record => {
82+ return Object . values ( record . components )
83+ } )
84+ . flat ( )
85+ . filter ( Component => {
86+ return (
87+ Component . cid === undefined && typeof Component === 'function'
88+ )
89+ } )
7990 } ,
8091 linkPrefetch ( ) {
81- const { route } = this . $router . resolve ( this . to )
92+ const route = this . $router . resolve ( this . to )
8293
8394 if ( route . meta . __prefetched ) return
8495
8596 route . meta . __prefetched = true
8697
8798 if ( route . meta . prefetch !== false ) {
8899 // Prefetch route component
89- const components = this . getComponents ( )
100+ const components = this . getRouteComponents ( route )
90101 for ( const Component of components ) {
91102 this . $emit ( 'prefetch' , this . to )
92103 Component ( ) // eslint-disable-line new-cap
@@ -113,12 +124,13 @@ function installRouterPrefetch(
113124 }
114125 }
115126
116- Vue . component ( Link . name , Link )
127+ // `app.component(Link.name, Link)` will emit a warning
128+ app . _context . components [ Link . name ] = Link
117129}
118130
119131export {
120132 prefetch ,
121- // Export as `install` to make `Vue .use(require('vue-router-prefetch'))` work
133+ // Export as `install` to make `app .use(require('vue-router-prefetch'))` work
122134 installRouterPrefetch as install
123135}
124136
0 commit comments