鍍金池/ 問答/C++  HTML/ vue 父組件調(diào)用子組件方法出現(xiàn)的問題

vue 父組件調(diào)用子組件方法出現(xiàn)的問題

在子組件中定義了一個方法,現(xiàn)在在父組件中調(diào)用會出現(xiàn)一些問題,求各路高手幫助
部分代碼如下:

子組件中定義的方法:

methods:{
      getUEContent: function(){
       return this.editor.getContent();
      },
      setUEContent: function(something){
       this.editor.setContent(something);
      },
    }

父組件中先定義了一個對話框:

    <el-dialog title="新增菜單" size="small" v-model="addFormVisible2" :close-on-click-modal="false">
      <div>
        <el-button size="primary" type="info" icon="plus" @click="getContent2">獲取內(nèi)容</el-button>
        <NEditor :config=config2 ref="neditor"></NEditor>
      </div>
    </el-dialog>

默認(rèn)不顯示,addFormVisible2 = false;

然后設(shè)置一個按鈕:

<el-button type="primary" @click.native="editItem">打開</el-button>

點擊顯示對話框:

    editItem: function(){
      this.addFormVisible2 = true;
      this.$refs.neditor.setUEContent("這里是測試語句");
    },

測試會出現(xiàn)如下錯誤:
圖片描述

初學(xué)vue,求高手們指導(dǎo)

回答
編輯回答
維她命

已經(jīng)解決了。。只要在子組件定義的方法里加上this.$nextTick就行了,異步執(zhí)行的原因,子組件還沒渲染完就調(diào)用了方法就會這樣

2018年3月6日 23:31
編輯回答
幼梔

父組件不能直接調(diào)用子組件方法···
vue按照父子組件通信的方式
父組件可以傳一個控制參數(shù)到子組件 子組件監(jiān)聽這個props 來執(zhí)行方法 就可以了

2018年2月21日 21:11
編輯回答
孤酒

調(diào)用methods中的方法,直接使用 this.setContent,并且確保 setContent 方法在這個methods中定義過。 如果這個方法是組件庫的方法,那就看好文檔,根據(jù)文檔來寫

2017年4月13日 09:26
編輯回答
避風(fēng)港
 請問這里setUEContent: function(something){
   this.editor.setContent(something);
  },中editor是哪里定義的
2018年3月29日 07:51