Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is <router-view :key="$route.fullPath"> ?

I'm completely new to Vue.js and I think I have a bit of understanding of how a router works with things like:

<router-link to="/"> 

But I am not really understanding what the following line does:

<router-view :key="$route.fullPath"></router-view> 

I believe router-view by itself makes sure the content is displayed but what does the key part mean?

like image 369
Jordan Avatar asked Oct 17 '18 05:10

Jordan


People also ask

What does router view do?

The RouterView or router-view component is used to display the current route the user is at.

What is router link in vue?

May 13, 2020. The router-link component creates an <a> tag that's configured to work correctly with Vue router.

What is vue router push?

With Vue Router, you can use its router. push() function to programmatically navigate between routes on your site. You can either call push() with a string path, or with an object containing either the path or name of the route.

How do I use my vue3 vue router?

Installing Vue Router for Vue 3 Like many frameworks, Vue has its own CLI. So at first, you need to install it using NPM or Yarn. Then you can create a new project using the vue create <project-name> command. After executing it, the CLI prompts with several options as follows.

What is the use of $Route full path in react?

$route.fullPathis defined as The full resolved URL including query and hash. If you bind keyto $route.fullPath, it will always "force a replacement"of the <router-view>element / component every time a navigation event occurs. As mentioned above, this is most probably done in order to trigger a transition / animation.

How to update the router-view when the router's path changes?

Just bind the component key property to a reactive data source. In the case of router changes, you can bind the router-view to the $route.fullPath as follows: Now, any time the full path changes, the router-view will be updated. This work is licensed under a Creative Commons Attribution 4.0 International License.

How to trigger router-view to update when Vue route changes?

Sometimes, your Vue route will change, but will not trigger your router-view to update. There is a simple way to tell the router-view, or any component, to update. Just bind the component key property to a reactive data source.

What is the path of the resolved URL?

The full resolved URL including query and hash. A string that equals the path of the current route, always resolved as an absolute path. e.g. "/foo/bar". Show activity on this post. path: A string that is equal to the path of the current route, always resolved as an absolute path.


1 Answers

See Special Attributes - key

It can also be used to force replacement of an element/component instead of reusing it. This can be useful when you want to:

  • Properly trigger lifecycle hooks of a component
  • Trigger transitions

$route.fullPath is defined as

The full resolved URL including query and hash.

If you bind key to $route.fullPath, it will always "force a replacement" of the <router-view> element / component every time a navigation event occurs.

As mentioned above, this is most probably done in order to trigger a transition / animation.

like image 141
Phil Avatar answered Oct 09 '22 13:10

Phil