twickstrom / vue-papa-parse

VueJS PapaParse Plugin
MIT License
16 stars 10 forks source link

How to use with Vue 3 script setup? #8

Open sajjadalis opened 2 years ago

sajjadalis commented 2 years ago

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

SebastienD11 commented 2 years ago

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");
vanngoh commented 1 year ago

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! 🙌

psgganesh commented 3 months ago

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