I’m new to Vue.js and trying to bind option data from the API response.
I have written axios call from the mounted() and assigned companies form the response but I’m getting defined error as per below.
JavaScript
x
3
1
373:11 error 'companies' is not defined no-undef
2
444:7 error 'companies' is not defined no-undef
3
I have assigned the API response data to the company but why it’s not working so can someone guide.
Template Part:
JavaScript
1
31
31
1
<template>
2
3
.
4
5
<validation-provider
6
#default="validationContext"
7
name="Company"
8
rules="required"
9
>
10
<b-form-group
11
label="Company"
12
label-for="company"
13
:state="getValidationState(validationContext)"
14
>
15
<v-select
16
v-model="userData.company"
17
:dir="$store.state.appConfig.isRTL ? 'rtl' : 'ltr'"
18
:options="companies"
19
:clearable="false"
20
input-id="company"
21
/>
22
<b-form-invalid-feedback :state="getValidationState(validationContext)">
23
{{ validationContext.errors[0] }}
24
</b-form-invalid-feedback>
25
</b-form-group>
26
</validation-provider>
27
28
..
29
30
</template>
31
Script Part:
JavaScript
1
145
145
1
<script>
2
import {
3
BSidebar, BForm, BFormGroup, BFormInput, BFormInvalidFeedback, BButton,
4
} from 'bootstrap-vue'
5
import { useToast } from 'vue-toastification/composition'
6
import ToastificationContent from '@core/components/toastification/ToastificationContent.vue'
7
import { ValidationProvider, ValidationObserver } from 'vee-validate'
8
import { ref } from '@vue/composition-api'
9
import { required, alphaNum, email } from '@validations'
10
import formValidation from '@core/comp-functions/forms/form-validation'
11
import Ripple from 'vue-ripple-directive'
12
import vSelect from 'vue-select'
13
import countries from '@/@fake-db/data/other/countries'
14
import store from '@/store'
15
import axios from '@/libs/axios'
16
17
export default {
18
components: {
19
BSidebar,
20
BForm,
21
BFormGroup,
22
BFormInput,
23
BFormInvalidFeedback,
24
BButton,
25
vSelect,
26
27
// Form Validation
28
ValidationProvider,
29
ValidationObserver,
30
},
31
directives: {
32
Ripple,
33
},
34
model: {
35
prop: 'isAddNewUserSidebarActive',
36
event: 'update:is-add-new-user-sidebar-active',
37
},
38
props: {
39
isAddNewUserSidebarActive: {
40
type: Boolean,
41
required: true,
42
},
43
roleOptions: {
44
type: Array,
45
required: true,
46
},
47
planOptions: {
48
type: Array,
49
required: true,
50
},
51
},
52
data() {
53
return {
54
required,
55
alphaNum,
56
email,
57
countries,
58
}
59
},
60
mounted() {
61
const accessToken = JSON.parse(localStorage.getItem('userData'))
62
axios.get('companies/all', {
63
headers: {
64
Authorization: accessToken.accessToken,
65
},
66
})
67
.then(
68
response => {
69
companies = response.data
70
},
71
)
72
},
73
setup(props, { emit }) {
74
const blankUserData = {
75
name: '',
76
username: '',
77
email: '',
78
password: '',
79
role: null,
80
currentPlan: null,
81
company: '',
82
country: '',
83
contact: '',
84
}
85
86
const userData = ref(JSON.parse(JSON.stringify(blankUserData)))
87
const resetuserData = () => {
88
userData.value = JSON.parse(JSON.stringify(blankUserData))
89
}
90
const toast = useToast()
91
92
// const companies = companies
93
94
const onSubmit = () => {
95
store.dispatch('app-user/addUser', userData.value)
96
.then(
97
response => {
98
if (response.status === 1) {
99
emit('refetch-data')
100
emit('update:is-add-new-user-sidebar-active', false)
101
102
toast({
103
component: ToastificationContent,
104
props: {
105
title: response.message,
106
icon: 'CheckIcon',
107
variant: 'success',
108
},
109
})
110
} else {
111
toast({
112
component: ToastificationContent,
113
props: {
114
title: response.message,
115
icon: 'InfoIcon',
116
variant: 'danger',
117
},
118
})
119
}
120
},
121
error => {
122
console.log(error)
123
},
124
)
125
}
126
127
const {
128
refFormObserver,
129
getValidationState,
130
resetForm,
131
} = formValidation(resetuserData)
132
133
return {
134
userData,
135
onSubmit,
136
137
refFormObserver,
138
getValidationState,
139
resetForm,
140
companies,
141
}
142
},
143
}
144
</script>
145
Advertisement
Answer
You need to define companies
in data function:
JavaScript
1
10
10
1
data() {
2
return {
3
required: false,
4
alphaNum : null,
5
email: '',
6
countries: [],
7
companies: []
8
}
9
},
10
then in hook you need to use this
:
JavaScript
1
15
15
1
async mounted() {
2
const accessToken = JSON.parse(localStorage.getItem('userData'))
3
await axios.get('companies/all', {
4
headers: {
5
Authorization: accessToken.accessToken,
6
},
7
})
8
.then(
9
response => {
10
this.companies = response.data
11
this.companies = this.companies.map(c => c.label)
12
},
13
)
14
},
15