我有这个组件
<script>
const serviceName = 'events'
import { mapState } from 'vuex'
import crud from './mixins/crud'
export default {
mixins: [crud],
data() {
return {
serviceName: serviceName,
apiBaseUri: '/api/v1/' + serviceName,
}
},
computed: {
...mapState({
events: state => state.events.data,
}),
},
mounted() {
this.boot()
},
}
</script>
它定义了一个serviceName,我也需要在这个crud混合中使用它:
import { mapActions, mapMutations, mapState } from 'vuex'
export default {
data: function() {
return {
loading: {
environment: false,
table: false,
},
}
},
computed: {
...mapState({
form: state => state.events.form,
environment: state => state.environment,
}),
},
methods: {
...mapActions(serviceName, ['load']),
...mapMutations(serviceName, [
'setDataUrl',
'setStoreUrl',
'setErrors',
'setFormData',
'storeFormField',
]),
isLoading() {
return this.loading.environment || this.loading.table
},
boot() {
this.setDataUrl(this.apiBaseUri)
this.setStoreUrl(this.apiBaseUri)
this.load()
},
back() {
this.$router.back()
},
storeModel() {
this.store().then(() => {
this.load()
this.back()
this.clearForm()
})
},
},
}
问题是我总是收到"serviceName is not defined“错误消息,因为serviceName用在mapActions()和mapMutations()中。
错误发生在
import crud from './mixins/crud'
它完全忽略了像window.serviceName这样的东西,我也尝试过。
发布于 2018-10-16 02:26:49
我能想到的最简单的解决方案是将serviceName
定义移到另一个文件中。例如..。
// constants.js
export const SERVICE_NAME = 'events'
然后,您可以在需要的地方导入它,例如
// in your component, your mixin, etc
import { SERVICE_NAME as serviceName } from './constants'
https://stackoverflow.com/questions/52826951
复制