I am using nuxt v2.14.1 with typescript and nuxt-property-decorator package.
I am facing lots of issues.
I am not able to set data from fetch() or asyncData.
console.log(this.fruits) // => undefined console.log(response.data);// => [{},..} this.fruits = response.data; // => this does not set fruits
Vue dev tools show fruits as an empty array too.
Can anyone help me understand what's happening and how to solve it?
Here is the code using fetch:
@Component({
layout: 'AppLayout',
async fetch({ query, store }: Context) {
const self = this;
const model = new Car();
model.setFilters(query);
const response = await model.$api.$get('/new/cars').catch(() => {
store.$showError('Unable to fetch cars');
});
if (response) {
// @ts-ignore
self.cars = Car.collect(response.data as Partial<Car>[]);
}
},
})
export default class index extends Vue {
cars: Car[] = [];
}
The above code works if I just rename fetch to mounted.
Also, when using asyncData, the code seems to work, and I see my car list, and then, when page loading completes, page autmatically becomes empty even though I can see cars listed in Vue dev tools.
Update: Not using nuxt context in fetch parameter seems to work. No idea why though.