Encountering a strange problem with vue-router
.
Developed a simple CRUD app for managing users.
The app includes four main views:
- User list and a create button
- Create view with a form child component that fetches field data from an API within the created() lifecycle hook.
- User show view
- User update view with a similar form child component.
Issue arises when navigating to edit page of different users. The form displays values of the first user (id: 1) without making any new Ajax request upon switching users.
Seems like the component is being reused.
Attempted giving unique :key
values to form child components, but no luck.
<router-view :key="$route.fullPath"></router-view>
Setting key on router-view also did not resolve the issue...
Refreshing browser correctly displays form content
Providing a brief overview below:
Defined routes:
import Index from "./views/Index";
import Show from "./views/Show";
import Edit from "./views/Edit";
import Create from "./views/Create";
export default [
{
path: '/admin/users',
name: 'users.index',
component: Index
},
{
path: '/admin/users/create',
name: 'users.create',
component: Create
},
{
path: '/admin/users/:id',
name: 'users.show',
component: Show,
props: true
},
{
path: '/admin/users/:id/edit',
name: 'users.edit',
component: Edit,
props: true
},
];
Edit component (Create has same structure without id):
<template>
<ResourceForm
resource="user"
:resource-id="id"
@cancel="$router.push({name: 'users.index'})"
/>
</template>
<script>
import ResourceForm from "../../../components/ResourceForm";
export default {
components: {ResourceForm},
props: ['id'],
}
</script>
ResourceForm component:
<template>
<form @submit.prevent="submitResourceForm">
<component
v-for="(field, index) in fields"
:key="field.name + index"
:ref="`${field.name}-field`"
:is="field.component"
:field="field"
:validation-errors="getValidationErrors(field)"
/>
<div class="mt-8 border-t border-gray-200 pt-5">
<div class="flex justify-end">
<span class="inline-flex rounded-md shadow-sm">
<button type="button" @click="$emit('cancel')"
class="py-2 px-4 border border-gray-300 rounded-md text-sm leading-5 font-medium text-gray-700 hover:text-gray-500 focus:outline-none focus:border-blue-300 focus:shadow-outline-blue active:bg-gray-50 active:text-gray-800 transition duration-150 ease-in-out">
{{this.cancelText}}
</button>
</span>
<span class="ml-3 inline-flex rounded-md shadow-sm">
<button type="submit"
class="btn">
{{this.submitText}}
</button>
</span>
</div>
</div>
</form>
</template>
<script>
import _ from 'lodash';
import api from "../lib/api";
import Form from "../lib/mixins/Form";
export default {
mixins: [Form],
props: {
resource: {
type: String,
required: true
},
resourceId: {
required: false,
default: null
},
cancelText: {
type: String,
default: 'Cancel'
},
submitText: {
type: String,
default: 'Submit'
},
},
data() {
return {
fields: []
}
},
watch: {
'$route': function () {
console.log('route changed');
this.fetchResourceForm();
}
},
created() {
this.fetchResourceForm();
},
methods: {
async fetchResourceForm() {
let route;
if (this.resourceId !== null) {
route = Cms.route('cms.api.forms.show', {
resource: this.resource,
id: this.resourceId
});
} else {
route = Cms.route('cms.api.forms.new', {resource: this.resource});
}
const response = await api(route);
this.fields = response.data.data.fields;
},
async submitResourceForm() {
const formData = this.getFormData();
try {
const foo = {
...Cms.route('cms.api.forms.store', _.pickBy({
resource: this.resource,
id: this.resourceId
})),
data: formData
};
const response = await api(foo);
this.$emit('success', response.data.data);
} catch (error) {
if (error.response.status === 422) {
this.validationErrors = error.response.data.errors;
Cms.flash('error', 'There are validation errors in the form.')
}
}
}
}
}
</script>
The Cms.route method generates API routes and does not relate to vue-router functionalities.