比如我在父組件中傳一個方法,但是在子組件中執(zhí)行,應(yīng)該怎么做呢?不使用$emit方式
1 回答

繁星點點滴滴
TA貢獻1803條經(jīng)驗 獲得超3個贊
方法有多種,比較常用的是在子組件的porps
定義個Function參數(shù),父組件把方法當(dāng)參數(shù)傳入
子組件代碼片段
export default { name: 'Children', props: { parentMethod: Function, }, methods: { onClick() { this.parentMethod(); },
父組件代碼片段
<template> <children :parent-method="testMethod"></children></template><script> export default { methods: { testMethod() { console.log('test'); },
這樣當(dāng)子組件觸發(fā)onClick
就調(diào)用父組件的testMethod
,流程雖然比不上$on
+$emit
清晰,但是可維護性還是挺高的
添加回答
舉報
0/150
提交
取消