現在のコンポーネント インスタンスを取得する vue3 メソッド getCurrentInstance

現在のコンポーネント インスタンスを取得する vue3 メソッド getCurrentInstance

vue3 では、セットアップは beforeCreate および created ライフ サイクル フックの周囲で実行されます。セットアップの実行時にコンポーネント オブジェクトは作成されていません。現時点では、これを使用して data/computed/methods/props にアクセスすることはできません。getCurrentInstance 関数を使用できます現在のコンポーネントのインスタンス オブジェクト (現在の vue インスタンス オブジェクト) を返します。

import {
    
     getCurrentInstance, onMounted, ref } from 'vue'
export default ({
    
    
    components:{
    
    
        Header
    },
    setup(props, context) {
    
    
        const {
    
     ctx } = getCurrentInstance()
        onMounted(()=>{
    
    
           ctx.$http.get('/user')
        })
        return {
    
    
        }
    }

おすすめ

転載: blog.csdn.net/Stars_in_rain/article/details/122468824