<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                >[success] # v-for 中的 Ref 數組 ~~~ 1.在 Vue 2 中,在 v-for 里使用的 ref attribute 會用 ref 數組填充相應的 $refs property。 當存在嵌套的 v-for 時,這種行為會變得不明確且效率低下。 2.在 Vue 3 中,這樣的用法將不再在 $ref 中自動創建數組。要從單個綁定獲取多個 ref,請將 ref 綁定到 一個更靈活的函數上 (這是一個新特性) ~~~ >[info] ## vue2.x ~~~ 1.通過案例理解官網說的第一條'vue2.x' 幫我們把 'v-for' 綁定$refs將會自動生成一個refs的數組,自動將 一樣的ref綁定的值的數據歸并在一個數組中 ~~~ ~~~ <template> <div> <div v-for="(outter, outterIndex) in 5" :key="outterIndex" ref="OutTest"> <div v-for="(inner, innerIndex) in 5" :key="innerIndex" ref="inItem"> <!-- {{ item }} --> </div> </div> </div> </template> <script> export default { name: 'Test', mounted() { console.log(this.$refs) console.log(this.$refs.test) } } </script> ~~~ * 如圖 ![](https://img.kancloud.cn/df/b3/dfb352a874c59e0e587368c576b295df_970x76.png) >[danger] ##### 看一個附加小問題 ~~~ 1.修改一下上面的案例將內外層的ref 命名都改成一樣的'inItem',由于這友好將統一名字的ref歸并在一個數組 的機制導致內外循環綁定的ref都在了一起 ~~~ ~~~ <template> <div> <div v-for="(outter, outterIndex) in 5" :key="outterIndex" ref="inItem"> <div v-for="(inner, innerIndex) in 5" :key="innerIndex" ref="inItem"> <!-- {{ item }} --> </div> </div> </div> </template> <script> export default { name: 'Test', mounted() { console.log(this.$refs) console.log(this.$refs.test) } } </script> <style></style> ~~~ * 如圖 ![](https://img.kancloud.cn/99/88/9988bafc20210463d3eb0e8466fef99a_1104x147.png) >[info] ## vue3.x 使用 ~~~ 1.vue3并不會自動幫我們生成refs數組,只能得到最后一個元素的ref,想v-for 和 ref搭配需要手動去 創建變量來接受,ref 綁定一個方法 2.這個用來接收的變量不一定非要是數組 ~~~ ~~~ <template> <div> <p v-for="(item, index) in 5" :key="index+100" :ref="setItemRef">{{item}}</p> <p v-for="(item, index) in 5" :key="index" ref="test">{{item}}</p> <a ref="a1">1</a> <a ref="a2" @click="print">2</a> </div> </template> <script> export default { name: 'Test', mounted() { console.log(this.$refs) console.log(this.$refs.test) // 按照vue2的寫法取出來是v-for渲染的最后一個dom元素 }, data() { return { itemRefs: [] } }, methods: { print() { console.log(this.itemRefs) }, setItemRef(el) { this.itemRefs.push(el) }, } // beforeUpdate() { // this.itemRefs = [] // }, // updated() { // console.log(this.itemRefs, 11111) // } } </script> <style></style> ~~~ * 圖一 ![](https://img.kancloud.cn/03/2c/032c7488825e9dd0208842bbd57bce2b_389x65.png) * 圖二看'itemRefs' ![](https://img.kancloud.cn/40/87/4087f21e7235d252bfdfa4b4c9fe3515_305x63.png) >[danger] ##### 注意 ~~~ 1.itemRefs 上面定義的是數組,也可以是對象 itemRefs: {} setItemRef(el) { let index = parseInt(Math.random(1) * 1000); this.itemRefs[index] = el; }, ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看