英文:
I am trying to get location name with location id using vue js
问题
抱歉,我明白你要求的,以下是代码的翻译部分:
locationList.vue
<template>
<div class="q-gutter-md q-pa-sm col-md-3">
<q-select
v-model="location_id"
filled
label="Locations"
:options="api.locations"
bg-color="white"
name="location_id"
dense
emit-value
map-options
option-label="name"
option-value="id"
@update:model-value="addLocation"
/>
</div>
</template>
script
methods: {
getLocations () {
axios.get(process.env.API_URL + '/lookups/locations')
.then(({ data: { items } }) => {
this.api.locations = items
})
.catch((error) => {
console.log(error)
})
},
addLocation () {
let locationName = null
if (this.location_id != null) {
locationName = this.api.locations.name
}
store.dispatch('location/storeLocation', {
location_id: this.location_id,
locations: locationName
})
}
}
请注意,代码中的注释和变量名保持原样,未进行翻译。
英文:
When I select to location I want also to get the location name but unfortunatly getting location name wrong with location id please help me how can i solved thank u.
locationList.vue
<template>
<div class="q-gutter-md q-pa-sm col-md-3">
<q-select
v-model="location_id"
filled
label="Locations"
:options="api.locations"
bg-color="white"
name="location_id"
dense
emit-value
map-options
option-label="name"
option-value="id"
@update:model-value="addLocation"
/>
</div>
</template>
script
methods: {
getLocations () {
axios.get(process.env.API_URL + '/lookups/locations')
.then(({ data: { items } }) => {
this.api.locations = items
})
.catch((error) => {
console.log(error)
})
},
addLocation () {
let locationName = null
if (this.location_id != null) {
locationName = this.api.locations.name
}
store.dispatch('location/storeLocation', {
location_id: this.location_id,
locations: locationName
})
}
}
答案1
得分: 1
以下是您要求的代码部分的中文翻译:
假设 api.locations 是一个对象数组,您在 addLocation 函数中并没有搜索位置名称,同时您确定 location_id 已经被设置了吗?如果没有设置,您只是在触发一个带有空 locationName 的存储事件。
对于您来说,以下代码可能更适合:
addLocation () {
let locationName = null
if (this.location_id) {
locationName = this.api.locations.find(item => item.id === this.location_id).name;
}
if(locationName) {
store.dispatch('location/storeLocation', {
location_id: this.location_id,
locations: locationName
})
}
}
有关在数组中搜索更多信息,请参考此答案。
英文:
assuming that api.locations is an array of objects you're not searching for the location name in addLocation. also are you sure that location_id is being set? if it is not you're just dispatching a store event with a null locationName
something like this would work better for you
addLocation () {
let locationName = null
if (this.location_id) {
locationName = this.api.locations.find(item => item.id === this.location_id).name;
}
if(locationName) {
store.dispatch('location/storeLocation', {
location_id: this.location_id,
locations: locationName
})
}
}
more information on searching arrays from this answer
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论