Here is the problem. Root view:
JavaScript
x
22
22
1
<template>
2
<div class="home">
3
<Grid :rows=3 :cols=4>
4
<GridItem :x=1 :y=1 />
5
<GridItem :x=2 :y=1 />
6
</Grid>
7
</div>
8
</template>
9
10
<script>
11
import Grid from '@/components/Grid'
12
import GridItem from '@/components/GridItem'
13
14
export default {
15
name: 'Home',
16
components: {
17
Grid,
18
GridItem
19
}
20
}
21
</script>
22
Grid component (container):
JavaScript
1
21
21
1
<template>
2
<div class="grid">
3
<slot v-bind="testData"></slot>
4
</div>
5
</template>
6
7
<script>
8
export default {
9
name: 'Grid',
10
data() {
11
return {
12
testData: 'test'
13
}
14
},
15
props: {
16
rows: Number,
17
cols: Number
18
}
19
}
20
</script>
21
And finally grid item component:
JavaScript
1
16
16
1
<template v-slot="slotProps">
2
<div class="grid-item">
3
{{x}} {{y}} {{slotProps.testData}}
4
</div>
5
</template>
6
7
<script>
8
export default {
9
name: 'GridItem',
10
props: {
11
x: Number,
12
y: Number
13
}
14
}
15
</script>
16
I’m getting the error: GridItem.vue Uncaught (in promise) TypeError: Cannot read property ‘testData’ of undefined. I’m cracked my head trying to understand what’s going wrong. Need help, thanks for your time.
Advertisement
Answer
Get your slot props in Home
component:
JavaScript
1
9
1
<template>
2
<div class="home">
3
<Grid :rows=3 :cols=4 v-slot="{ testData }">
4
<GridItem :x=1 :y=1 :test-data="testData" />
5
<GridItem :x=2 :y=1 :test-data="testData" />
6
</Grid>
7
</div>
8
</template>
9