Issue
I created a project in Vue.js 3 and TypeScript.
router.js
{
path: "/app/:id",
name: "Detail",
component: Detail,
props: true
},
App.js
<script lang="ts">
...
onMounted(() => {
const id = $route.params.id;
...
});
But this results in an error:
"Cannot find name '$route'."
What am I doing wrong?
Solution
Vue Router 4.x provides useRoute()
for that:
import { useRoute } from 'vue-router'
export default {
setup() {
const route = useRoute()
onMounted(() => {
const id = route.params.id
})
}
}
Answered By - tony19
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.