vue-papa-parse icon indicating copy to clipboard operation
vue-papa-parse copied to clipboard

How to use with Vue 3 script setup?

Open sajjadalis opened this issue 2 years ago • 3 comments

I'm getting this error. Uncaught TypeError: Cannot read properties of undefined (reading '$papa')

<script setup>
import { ref, onMounted } from "vue"

onMounted(() => {
    this.$papa.parse(file.value, {
	header: true,
	skipEmptyLines: true,
	complete: (results) => {
	content.value = results;
	parsed.value = true;
	},
    });
})
</script>

Tried importing VuePapaParse inside component but getting similar error. Uncaught TypeError: VuePapaParse.parse is not a function

Thanks

sajjadalis avatar Jul 09 '22 19:07 sajjadalis

I was facing the same issue. Vue-Papa-Parse does not provide the value $papa, and we cannot use this inside setup() with Vue3 composition API.

I found the "solution" within this topic: https://forum.vuejs.org/t/how-to-use-globalproperties-in-vue-3-setup-method/108387/4

Manually provide $papa from your main.ts

app.provide('$papa', app.config.globalProperties.$papa)

And in your component, inject it this way:

import { [...], inject } from "vue";
[...]
const $papa = inject("$papa");

SebastienD11 avatar Jul 15 '22 21:07 SebastienD11

An alternative way for this can be

<script setup>
import { ref, onMounted } from "vue"
import * as papa from "papaparse"

onMounted(() => {
    papa.parse(file.value, {
	header: true,
	skipEmptyLines: true,
	complete: (results) => {
	content.value = results;
	parsed.value = true;
	},
    });
})
</script>

Hope this helps! 🙌

vanngoh avatar May 26 '23 08:05 vanngoh

@vanngoh your comment solves the issue, this issue can be closed 🙌🙏

psgganesh avatar Jul 04 '24 02:07 psgganesh