반응형
VueJS | 컴포넌트 내의 라우터 파라미터에 액세스하는 방법
Entry 컴포넌트에서 파라미터:id에 액세스하려고 했습니다.소품으로 해봤는데 안 되네.감 잡히는 게 없어요?아무것도 못 찾겠어.
export default new Router({
routes: [{
path: '/entry/:id',
name: 'Entry',
component: Entry
}]
})
감사합니다.
소주의 답은 맞지만, 저는 '소품을 이용해 공유기를 분리한다'는 의사들의 표현 방식을 사용하는 경향이 있습니다.
이를 위해서는 루트에 true 옵션을 추가하고 컴포넌트에 속성을 정의하면 됩니다.
따라서 경로에서 다음과 같은 것이 있습니다.
export default new Router({
routes: [{
path: '/entry/:id',
name: 'Entry',
component: Entry,
props: true
}]
})
그런 다음 컴포넌트에 다음과 같은 소품을 추가합니다.
Vue.component('Entry', {
template: '<div>ID: {{ id}}</div>',
props:['id']
})
이 모든 내용은 문서 "Passing Proposing Product to Route Components"에서 확인할 수 있습니다.
를 간단하게 사용해 주세요.$route.params.id
또는this.$route.params.id
.
자세한 내용은 이쪽 : https://router.vuejs.org/guide/essentials/dynamic-matching.html
소품 사용을 검토해야 합니다.https://router.vuejs.org/en/essentials/passing-props.html
컴포넌트에서 사용할 수 있습니다.
this.$route.params.id
html로 표시<div>{{$route.params.id}}</div>
스크립트에 포함this.$route.params.id
<template>
<div>
<p>{{id}}</p>
</div>
</template>
<script>
export default {
data() {
return {
id:this.$route.params.id
}
},
}
</script>
언급URL : https://stackoverflow.com/questions/48446709/vuejs-how-to-get-access-to-router-parameter-in-my-component
반응형
'itsource' 카테고리의 다른 글
JVM 옵션 - XSS - 정확히 어떤 역할을 합니까? (0) | 2022.08.19 |
---|---|
Vue.js에서 Foundation 이벤트를 청취하시겠습니까? (0) | 2022.08.19 |
Vuejs 3의 변수로 Vuex에 저장된 값을 전달하는 방법(Quasar 2) (0) | 2022.08.17 |
중첩된 데이터를 사용한 VueJ 입력 바인딩 (0) | 2022.08.17 |
after 모드: 'history' vue 라우터가 작동하지 않음 (0) | 2022.08.17 |