使用 Vue 3.2,我的印象是我遵循檔案
我正在向子組件發送一組物件,作為回報,我試圖向父組件發出一個事件,以便從陣列中洗掉一個物件(由 id 標識)。
// parent
<template>
<TheAccountLines :lines=recurrents_credits @goaway(id)="goaway(id)" />
</template>
<script setup>
import { find } from "lodash"
// this doesn't get called at all, just putting it here to show that the function exists.
const goaway = (id) => {
var line = find (state.accounts, {id:id})
console.log(line)
}
</script>
// child component
<template>
<div v-for="line in lines" :key="line.id">
<span @click="goaway(line.id)" >X</span>
{{ line.foo) }}
{{ line.bar}}
</div>
</template>
<script setup>
const props = defineProps({ 'lines': Array)}
const emit = defineEmits(['goaway'])
const goaway = (id) => emit('goaway', id)
</script>
頁面加載后,我在控制臺中收到此訊息:無關的非發射事件偵聽器 (goaway(id)) 已傳遞給組件,但無法自動繼承,因為組件呈現片段或文本根節點。如果偵聽器僅用作組件自定義事件偵聽器,請使用“emits”選項宣告它。
我理解“發射”選項是defineProps. 但我一定是錯的。
uj5u.com熱心網友回復:
我認為問題是:你應該使用@goaway,但不是@goaway(id)。
部分模板:
<template>
<!--- THIS IS WRONG, SEE UPDATE BELOW -->
<TheAccountLines :lines="recurrents_credits" @goaway(id)="goaway(id)" />
</template>
順便說一句,您可以使用onGoawayprop instand @goaway:
// parent component
<template>
<TheAccountLines :lines="recurrents_credits" @goaway="goaway" />
</template>
// child component
<script setup>
const props = defineProps(['onGoaway'])
const emitDelete = (id) => { props.goaway(id) }
</script>
uj5u.com熱心網友回復:
像往常一樣,經過數小時的搜索,我在這里提出問題 5 分鐘后找到了答案。我不會洗掉這個問題(除非溫和我喜歡這樣做)。
錯誤出現在父級中:不要使用 emit 中的引數:
檔案
<TheAccountLines :lines=recurrents_credits @goaway="goaway" />
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/485890.html
標籤:javascript 成分 Vuejs3
上一篇:將二維陣列轉換為物件陣列
