实际上,为什么最好使用:to="{ name: 'home'}"
,而不是简单地使用to:="/"
<template>
<h1>Vue 2:</h1>
<router-link to="/">Home</router-link>
<router-link :to="{ name: 'home'}">Home</router-link>
<router-view/>
</template>
实际上,为什么最好使用:to="{ name: 'home'}"
,而不是简单地使用to:="/"
<template>
<h1>Vue 2:</h1>
<router-link to="/">Home</router-link>
<router-link :to="{ name: 'home'}">Home</router-link>
<router-view/>
</template>
根据路由文档中的named routes,以下是使用名称而不是路径的优势:
- 没有硬编码的URL
- 参数的自动编码/解码
- 防止您在URL中出现拼写错误
- 绕过路径排名(例如,显示)