<router-link>
<router-link> is the component for enabling user navigation in a router-enabled app. The target location is specified with the to prop. It renders as an <a> tag with correct href by default, but can be configured with the tag prop. In addition, the link automatically gets an active CSS class when the target route is active.
<router-link> is preferred over hard-coded <a href="..."> for the following reasons:
It works the same way in both HTML5 history mode and hash mode, so if you ever decide to switch mode, or when the router falls back to hash mode in IE9, nothing needs to be changed.
In HTML5 history mode,
router-linkwill intercept the click event so that the browser doesn't try to reload the page.When you are using the
baseoption in HTML5 history mode, you don't need to include it intoprop's URLs.
Props
to
type:
string | Locationrequired
Denotes the target route of the link. When clicked, the value of the
toprop will be passed torouter.push()internally, so the value can be either a string or a location descriptor object.<!-- literal string --> <router-link to="home">Home</router-link> <!-- renders to --> <a href="home">Home</a> <!-- javascript expression using `v-bind` --> <router-link v-bind:to="'home'">Home</router-link> <!-- Omitting `v-bind` is fine, just as binding any other prop --> <router-link :to="'home'">Home</router-link> <!-- same as above --> <router-link :to="{ path: 'home' }">Home</router-link> <!-- named route --> <router-link :to="{ name: 'user', params: { userId: 123 }}">User</router-link> <!-- with query, resulting in `/register?plan=private` --> <router-link :to="{ path: 'register', query: { plan: 'private' }}">Register</router-link>replace
type:
booleandefault:
false
Setting
replaceprop will callrouter.replace()instead ofrouter.push()when clicked, so the navigation will not leave a history record.<router-link :to="{ path: '/abc'}" replace></router-link>append
type:
booleandefault:
false
Setting
appendprop always appends the relative path to the current path. For example, assuming we are navigating from/ato a relative linkb, withoutappendwe will end up at/b, but withappendwe will end up at/a/b.<router-link :to="{ path: 'relative/path'}" append></router-link>tag
type:
stringdefault:
"a"
Sometimes we want
<router-link>to render as another tag, e.g<li>. Then we can usetagprop to specify which tag to render to, and it will still listen to click events for navigation.<router-link to="/foo" tag="li">foo</router-link> <!-- renders as --> <li>foo</li>active-class
type:
stringdefault:
"router-link-active"
Configure the active CSS class applied when the link is active. Note the default value can also be configured globally via the
linkActiveClassrouter constructor option.exact
type:
booleandefault:
false
The default active class matching behavior is inclusive match. For example,
<router-link to="/a">will get this class applied as long as the current path starts with/a/or is/a.One consequence of this is that
<router-link to="/">will be active for every route! To force the link into "exact match mode", use theexactprop:<!-- this link will only be active at `/` --> <router-link to="/" exact>Check out more examples explaining active link class live.
event
2.1.0+
type:
string | Array<string>default:
'click'
Specify the event(s) that can trigger the link navigation.
exact-active-class
2.5.0+
type:
stringdefault:
"router-link-exact-active"
Configure the active CSS class applied when the link is active with exact match. Note the default value can also be configured globally via the
linkExactActiveClassrouter constructor option.
Applying Active Class to Outer Element
Sometimes we may want the active class to be applied to an outer element rather than the <a> tag itself, in that case, you can render that outer element using <router-link> and wrap the raw <a> tag inside:
<router-link tag="li" to="/foo">
<a>/foo</a>
</router-link>
In this case the <a> will be the actual link (and will get the correct href), but the active class will be applied to the outer <li>.