我正在尝试使用Vue.js通过位置ID获取位置名称。

huangapple go评论58阅读模式
英文:

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

&lt;template&gt;
	&lt;div class=&quot;q-gutter-md q-pa-sm col-md-3&quot;&gt;
		&lt;q-select
			v-model=&quot;location_id&quot;
			filled
			label=&quot;Locations&quot;
			:options=&quot;api.locations&quot;
			bg-color=&quot;white&quot;
			name=&quot;location_id&quot;
			dense
			emit-value
			map-options
			option-label=&quot;name&quot;
			option-value=&quot;id&quot;
			@update:model-value=&quot;addLocation&quot;
		/&gt;
	&lt;/div&gt;
&lt;/template&gt;

script

methods: {
		getLocations () {
			axios.get(process.env.API_URL + &#39;/lookups/locations&#39;)
				.then(({ data: { items } }) =&gt; {
					this.api.locations = items
				})
				.catch((error) =&gt; {
					console.log(error)
				})
		},
		addLocation () {
			let locationName = null
			if (this.location_id != null) {
				locationName = this.api.locations.name
			}
			store.dispatch(&#39;location/storeLocation&#39;, {
				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 =&gt; item.id === this.location_id).name;
    }
    if(locationName) {
        store.dispatch(&#39;location/storeLocation&#39;, {
            location_id: this.location_id,
            locations: locationName
        })
    }
}

more information on searching arrays from this answer

huangapple
  • 本文由 发表于 2023年2月10日 16:07:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75408404.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定