๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์›น๊ณต๋ทฐ

vue.js ๋ผ์šฐํ„ฐ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’ ์ฝ๊ธฐ null ์—๋Ÿฌ

by ์ด๋…ธํ‚ค_ 2023. 10. 25.

router-link to="/xxxx/detail"๋กœ ์ž‘์„ฑํ›„, 

๋ฐ›๋Š” ์ปดํฌ๋„ŒํŠธ์—์„œ this.$router.params.detail๋กœ ์ฝ์œผ๋‹ˆ ๊ณ„์† ์ฝํžˆ์ง€๊ฐ€ ์•Š์•˜๋‹ค.

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'detail')

router.js๋„ ์ œ๋Œ€๋กœ ์…‹ํŒ…์„ ํ–ˆ๋Š”๋ฐ ์™œ ์•ˆ๋˜๋‚˜ ํ•˜๋‹ค๊ฐ€ ๋ธ”๋กœ๊ทธ๊ธ€์„ ์ฝ๊ณ  ๋‚˜์„œ this.$route.params.detail๋กœ ์ฝ์œผ๋‹ˆ ์ž˜ ์ฝํžˆ๋”๋ผ.

๊ทธ๋ž˜์„œ chat gpt์—๊ฒŒ ์ฐจ์ด๊ฐ€ ๋ญ”์ง€ ๋ฌผ์–ด๋ดค๋‹ค.

 

Q: what is different this.$router.params.~~ and this.$route.params?

A:

  1. this.$route.params: This is the correct way to access the route parameters in Vue Router. It provides access to the dynamic route parameters defined in your route configuration. For example, if you have a route like /user/:id, you can access the id parameter using this.$route.params.id. (์ฆ‰, ๋‚ด๊ฐ€ ์›๋ž˜ ์‚ฌ์šฉํ•˜๋ ค๋˜ ์˜๋„์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์„ ์ฝ์„ ๋•Œ ์‚ฌ์šฉ๋จ)
  2. this.$router: The $router object is used for programmatic navigation and controlling the router instance. It provides methods like push, replace, and go, which allow you to navigate to different routes in your application programmatically. It does not directly provide access to route parameters like params. ($router๋Š” ๋ผ์šฐํ„ฐ ๊ฐ์ฒด ์ž์ฒด๋ฅผ ์ด์šฉํ•˜๋Š” ์šฉ๋„๋ผ๊ณ  ๋ณด๋ฉด๋ ๋“ฏ. ๋‚ด๋น„๊ฒŒ์ด์…˜(๋’ค๋กœ๊ฐ€๊ธฐ, ๋ฐ”๋กœ๊ฐ€๊ธฐ ๋“ฑ)์˜ ์šฉ๋„๋กœ ๋งŽ์ด ์“ฐ์ธ๋‹ค.)

 

๋Œ“๊ธ€