亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

vue.js 遇到的一個奇怪的問題,幫忙看看

vue.js 遇到的一個奇怪的問題,幫忙看看

海綿寶寶撒 2018-12-20 19:14:27
這是一個router中components的template:<template>  <div class="readerView">    <div id="area"></div>  </div></template><script>  export default {    name: 'readerView',    props: {      bookDetail: Object,    },    data () {      return {      }    },    watch: {      'bookDetail' : 'display'    },    methods: {      display() {        var area = document.getElementById("area");        console.log("in display, area: ", area);      }    },    mounted () {      var area = document.getElementById("area");      console.log("in mounted, area: ", area);    },  }</script><style>@import url("../style/basic.css");.readerView {  position: absolute;  width: 100%;  height: 100%;}</style>在該組件的mounted函數中調用document.getElementById("area");可以正常得到該divdisplay中同樣的調用document.getElementById("area");結果竟然返回空???
查看完整描述

1 回答

?
HUH函數

TA貢獻1836條經驗 獲得超4個贊

原因就是


watch: {

      'bookDetail' : 'display'

    },

執行的太早了,dom沒有渲染完全,改成


watch: {

      bookDetail(){

          setTimeout(()=>{

              this.display()

          },20);

      }

    }

另外為什么不用ref來取dom呢?


查看完整回答
反對 回復 2019-01-28
  • 1 回答
  • 0 關注
  • 468 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號