2

I want to bubble up an emit from a child component in ag-grid using vuejs 3. My strategy is to pass the parent component down with the composition API using <script setup> for use in the child component. The manual shows me how to do this with <setup>, but is silent using this syntactic sugar. The problem is that this is empty when passing the parent component. https://www.ag-grid.com/vue-data-grid/component-communication/

// Parent Component
<script setup>
const context = ref(null)

const emit = defineEmits(['action'])

onBeforeMount(() => {
  context.value = {
    componentParent: this
  }
})
</setup>

<template>
  <AgGridVue
    :context="context"
  />
</template>
// Child component (cellRenderer)
<script setup>
const props = defineProps({
  params: Object,
})

const fun = () => {
  params.context.componentParent.emit('action')
}
</script>

<template>
  <button @click="fun">Click me</button>
</template>

1 Answer 1

1

To access parent (function/reactive variable) you need to pass (function/reactive variable) inside context.value in onBeforeMount Hook. That means you simply have to define/pass whatever parameters you like to use.

// Parent Component
<script setup>
const context = ref({})


const parentFunction = (e) => {
    console.log("parent function called", e)
}

const parentData = ref(null)

onBeforeMount(() => {
  context.value = {
    // define function or reactive data you want to access from child
    // below data or function can be accessed by child
    parentData, 
    parentFunction,
  }
})
</setup>

<template>
  <AgGridVue
    :context="context"
  />
</template>

// Child component (cellRenderer)
<script setup>
const props = defineProps({
  params: Object,
})

// to access function of parent component
const fun = (params) => {
  params.context.parentFunction(params.data)
}

// to change parent component parentData
const fun = (params) => {
  params.context.parentData = "to change value"
}
</script>

<template>
  <button @click="fun(params)">Click me</button>
</template>
Sign up to request clarification or add additional context in comments.

2 Comments

You should add explanation.
How on earth did you find this information?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.