반응형
Vue 리소스 - http 메서드를 동적으로 결정합니다.
적절한 http 메서드를 동적으로 결정하고 단일 api 콜을 발신하고 싶습니다.그러나 메서드를 호출하면 예외가 발생합니다.
저는 제가 이 일을 하는 것보다 뭔가 잘못된 일을 하고 있다고 생각합니다.vue-resource
벌레요. 조언해 주실 분 있나요?고마워요.
예를 들어 다음과 같습니다.
let method = this.$http.post
if (this.model.id) {
method = this.$http.put
}
method(
this.url,
this.model,
options
).then(response => {
this.$router.push(this.redirect_to)
}).catch(response => {
console.log(`Error: ${response.statusText}`)
})
자바스크립트TypeError
"이것은 함수가 아닙니다"라는 메시지와 함께 느려집니다.
아래 코드는 동작하고 있습니다만, 조금 길게 설정되어 있습니다.
if (this.model.id) {
this.$http.put(
this.url,
this.model,
options
).then(response => {
this.$router.push(this.redirect_to)
}).catch(response => {
console.log(`Error: ${response.statusText}`)
})
} else {
this.$http.post(
this.url,
this.model,
options
).then(response => {
this.$router.push(this.redirect_to)
}).catch(response => {
console.log(`Error: ${response.statusText}`)
})
}
함수를 현재 컨텍스트에 바인딩해야 합니다.
let method = this.model.id ? this.$http.put.bind(this) : this.$http.post.bind(this)
또는 인덱서 접근 방식을 사용할 수도 있습니다.
let method = this.model.id ? 'put' : 'post'
this.$http[method](...).then(...)
언급URL : https://stackoverflow.com/questions/45804437/vue-resource-dynamically-determine-http-method
반응형
'programing' 카테고리의 다른 글
Vuex 저장소 상태가 변경될 때 Vue 구성 요소 속성을 업데이트하는 방법을 선택하십시오. (0) | 2022.08.30 |
---|---|
Array List의 마지막 값을 얻는 방법 (0) | 2022.08.30 |
CXF 또는 JAX-WS에서 생성된 웹 서비스 클라이언트에서 WSDL 위치를 지정할 필요가 없도록 하려면 어떻게 해야 합니까? (0) | 2022.08.30 |
Java의 정적 메서드에서 클래스 이름 가져오기 (0) | 2022.08.30 |
vuex에서 상수 변수를 생성하시겠습니까? (0) | 2022.08.30 |