如何将包含斜杠('/')的字符串作为参数传递给 vue.js 中的路由

How to pass a string containing slash ('/') as parameter to a route in vue.js

我正在使用 vue 路由器并且我有一个路由

{ path: '/my-route/:param?', component: MyComponent }

我有 link 到我的路线

<router-link :to="'/my-route/'+myParam">Link text</router-link>

如果 myParam 是一个包含 '/' 的字符串,如 'abc/def',它会导航到不存在的 link /my-route/abc/def。如何解决这个问题?

您必须使用 javascript 函数 encodeURIComponent(uri)

对 url 进行编码

更新你的路由器-link

<router-link :to="'/my-route/'+encodeURIComponent(myParam)">Link text</router-link>