我在Vue.js 3和TypeScript中创建了一个项目。
router.js
{
path: "/app/:id",
name: "Detail",
component: Detail,
props: true
},
App.js
<script lang="ts">
...
onMounted(() => {
const id = $route.params.id;
...
});
但这会导致错误:
"Cannot find name '$route'."
我哪里做错了?
3条答案
按热度按时间exdqitrt1#
Vue路由器4.x为此提供
useRoute()
:demo
0h4hbjxa2#
如果我们使用最新的Vue 3“脚本设置”SFC方式,则
lqfhib0f3#
您还可以使用组合API来编写自己的组合,当param为数组时,该组合提供更好的语法和优雅的处理。
组成
https://github.com/PrefectHQ/vue-compositions/tree/main/src/useRouteParam