강의

멘토링

커뮤니티

Inflearn Community Q&A

oopsdev's profile image
oopsdev

asked

[Web Development Full Stack Course] A week before starting a Vue.js project - From basics to practice

ListToDetail Pattern

this.$router params 방식 질문

Resolved

Written on

·

761

·

Edited

0

안녕하세요. 잘 안되는 부분이 있어서 질문하게 되었습니다. 현재 params로 넘길 시 id가 undefind라고 뜨고 query방식으로 넘길 시에는 데이터가 잘 넘어가는데 이유를 모르겠습니다..

github에 있는 코드를 그대로 붙여봐도 똑같아서 질문드립니다 ㅠ

 

params 방식

  • ListToDetailView.vue

console.log(customerId)
this.$router.push({ name: 'DetailView', params: { id: customerId } })
  • DetailView.vue

    console.log(this.$route)
    this.id = this.$route.params.id
    console.log(this.id)

- index.js에서 router 등록

  {
    path: '/template/p3/detail',
    name: 'DetailView',
    component: () =>
      import(
        /* webpackChunkName: "template", webpackPrefetch:true */ '../views/11_template/p3/DetailView.vue'
      )
  }

 

현재 log찍었을 때 router params에 id가 안들어있어서 goToDetail부분이 이상하다고 생각이드는데 혹시 몰라서 github에 있는 것을 그대로 붙여 쓰는데도 결과는 똑같더라고요 ㅠ 현재 customerId는 값이 들어오고있어요.

    goToDetail(customerId) {
      // this.$router.push({
      //   path: '/template/p3/detail',
      //   query: { id: customerId }
      // })

      console.log(customerId)
      this.$router.push({ name: 'DetailView', params: { id: customerId } })
      // http://localhost:8080/template/p3/detail
    },

 그리고 index.js는 github에 있는 파일과 코드 동일합니다.

 

query 방식

  • ListToDetailView.vue

this.$router.push({
  path: '/template/p3/detail',
  query: { id: customerId }
})
  • DetailView.vue

console.log(this.$route.query.id)     
this.id = this.$route.query.id

 

DetailView에서 EditView로 params로 넘길때도 똑같은 상황입니다 ㅠ

라우터에 :id 를 쓰면 되긴 하는데

 http://localhost:8080/template/p3/detail

이런 형식이 안되다보니..

vue.js

Answer 2

0

realprogrammers님의 프로필 이미지
realprogrammers
Instructor

일단 깃허브에 올라간 소스코드를 그대로 실행하면 정상적으로 오류 없이 실행이 됩니다. 깃허브 전체 코드 내려 받고, project 폴더에서 npm install --force 로 패키지 모듈 설치한 후 실행해 보면 params 방식이 정상적으로 되는 것을 확인할 수 있습니다.

얄라차개발자님이 안되는 이유를 파악하려면 조금 더 많은 정보가 필요합니다.

현재 package.json 내용을 알려주시면 제가 파악하는데 도움이 될것 같습니다.

oopsdev님의 프로필 이미지
oopsdev
Questioner

선생님~ package.json 첨부합니다~

{
  "name": "project",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "@fortawesome/fontawesome-svg-core": "^6.4.0",
    "@fortawesome/free-solid-svg-icons": "^6.4.0",
    "@fortawesome/vue-fontawesome": "^3.0.3",
    "@fullcalendar/core": "^6.1.8",
    "@fullcalendar/daygrid": "^6.1.8",
    "@fullcalendar/interaction": "^6.1.8",
    "@fullcalendar/timegrid": "^6.1.8",
    "@fullcalendar/vue3": "^6.1.8",
    "apexcharts": "^3.41.0",
    "axios": "^1.4.0",
    "bootstrap": "^5.2.3",
    "core-js": "^3.8.3",
    "exceljs": "^4.3.0",
    "file-saver": "^2.0.5",
    "vue": "^3.2.13",
    "vue-cookies": "^1.8.3",
    "vue-good-table-next": "^0.2.1",
    "vue-loading-overlay": "^6.0.3",
    "vue-router": "^4.2.2",
    "vue-sweetalert2": "^5.0.5",
    "vue3-apexcharts": "^1.4.1",
    "vuex": "^4.0.0",
    "vuex-persistedstate": "^4.1.0"
  },
  "devDependencies": {
    "@babel/core": "^7.12.16",
    "@babel/eslint-parser": "^7.12.16",
    "@vue/cli-plugin-babel": "~5.0.0",
    "@vue/cli-plugin-eslint": "~5.0.0",
    "@vue/cli-plugin-router": "~5.0.0",
    "@vue/cli-plugin-vuex": "~5.0.0",
    "@vue/cli-service": "~5.0.0",
    "@vue/eslint-config-standard": "^6.1.0",
    "eslint": "^7.32.0",
    "eslint-plugin-import": "^2.25.3",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-promise": "^5.1.0",
    "eslint-plugin-vue": "^8.0.3"
  },
  "eslintConfig": {
    "root": true,
    "env": {
      "node": true
    },
    "extends": [
      "plugin:vue/vue3-essential",
      "@vue/standard"
    ],
    "parserOptions": {
      "parser": "@babel/eslint-parser"
    },
    "rules": {
      "space-before-function-paren": "off"
    }
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not dead",
    "not ie 11"
  ]
}

0

realprogrammers님의 프로필 이미지
realprogrammers
Instructor

안녕하세요.

https://router.vuejs.org/guide/essentials/navigation.html

공식 문서에 나와 있는 것 처럼 params 가 되어야 하는데요.

제가 조금 더 확인해 본 후 답변 드리겠습니다.

일단 query 방식으로 진행 부탁드립니다

 

oopsdev님의 프로필 이미지
oopsdev
Questioner

아직도 해결을 못하고 있습니다... 프로젝트랑 다른건 라우터 버전인데 크게 영향이 없어보이는데 모르겠네요... ㅠ

realprogrammers님의 프로필 이미지
realprogrammers
Instructor

안녕하세요 router 4 부터는 router 부터는 아래와 같이 path에 params로 던질 파라미터를 반드시 정의해야 하는 것으로 변경이 되었네요. 이렇게 사용할 수 밖에 없습니다.

{
    path: '/template/p3/detail/:id',
    name: 'DetailView',
    component: () =>
      import(
        /* webpackChunkName: "template", webpackPrefetch:true */ '../views/11_template/p3/DetailView.vue'
      )
  }
oopsdev님의 프로필 이미지
oopsdev
Questioner

선생님~ 그러면 :id를 사용하면 그 강의때 말씀하신 url이 http://localhost:8080/template/p3/detail

이렇게 나오지 않고 id가 어떤건지 보여서 나오게 되면 query방식이나 params 방식 둘다 차이가 없는거져?
제가 강의를 들을때 생각했던거는 params방식과 query방식 url이 다르게 나온다고 생각했거든여...

realprogrammers님의 프로필 이미지
realprogrammers
Instructor

네 맞습니다

oopsdev님의 프로필 이미지
oopsdev
Questioner

그럼 둘중 편한거 쓰면 되겠네용 ㅎ 답변 감사합니다 :)

oopsdev's profile image
oopsdev

asked

Ask a question