創建一個方法
import { getCurrentInstance } from 'vue'; // expose public api export function useExpose(apis: Record<string, any>) { const instance = getCurrentInstance(); if (instance) { Object.assign(instance.proxy, apis); } }
將子組件內的方法,注冊進入該實例的proxy下
function open() { showPicker.value = true } useExpose({ open, close });
調用
const cityPicker = ref() const openCity = () => { cityPicker.value.open() }
以上代碼基本在是都compostion API setup中調用