vue 調用父頁面方法

1,使用$parent方法

//在父組件中
data(){
    return{
        a:1
    }
}
//在子組件中
this.$parent.a++

2,使用 依賴注入 (推薦使用)

//父組件
export default {
  name: 'App',
  provide(){
      return{
          say:this.say
      }
  },
  methods:{
      say(){
          alert("這是父頁面的方法");
      }
  }
}
//在子組件中
export default {
        inject:['say'],
        methods:{
         recv(){
             this.say();
         }
        }
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章