在 vue 中,为什么最好使用 :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在路由器文档中,以下是使用名称而不是路径的优点:
- No hardcoded URLs
- Automatic encoding/decoding of params
- Prevents you from having a typo in the url
- Bypassing path ranking (e.g. to display a )
关于vue.js - 为什么将 name 与 router-link 一起使用比仅将 to 与路径一起使用更好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74254375/