<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之旅 廣告
                ## **1. mixins作用** * 是一種分發 Vue 組件中可復用功能的非常靈活的方式。混入對象可以包含任意組件選項。當組件使用混入對象時,所有混入對象的選項將被混入該組件本身的選項。 * **有點類似于繼承的關系,定義組件的方法,數據供其他人使用,且數據和方法不共享** ## **2. 實例** ### **2.1 基礎小案例** **1. mixins-test.js 定義公用數據和方法** ~~~ // 定義了一個字面量類型的 組件模板對象 export const m1 = { data() { return { sonmsg: { name: '小頭兒子', age: 6 } } }, mounted(){ alert('mixins提供的mounted生命函數') }, methods: { myclick() { this.$emit('func', this.sonmsg) } } } ~~~ **2. mixins公用方法和數據** HelloWorld.vue ~~~ <template> <div class="hello"> {{sonmsg}} </div> </template> <script> import {m1} from '@/util/mixins-test' export default { name: "HelloWorld", props: { msg: String }, mixins: [m1] }; </script> ~~~ 彈出alert并顯示共有數據 ![](https://img.kancloud.cn/6d/76/6d762bcba26b60d4eb2d5535c2dc3ef8_736x212.png) ![](https://img.kancloud.cn/f5/26/f5268713163516ddf489853d2b8faac1_554x329.png) ### **2.2 數據和方法隔離** **新建一個testmixns.vue組件** * **引用了mixins共有數據的組件間,數據互相隔離不受影響** * 同一組件引入***methods,components*等,選項會被合并,沖突后,覆蓋mixins引入的方法** * **生命周期函數會依次執行,不會覆蓋** 重復的mounted方法,執行了兩次,但是about組件的mounted方法執行的是自己的info方法, #### 生命周期函數,不會覆蓋,依次執行(先執行mixins的) mixins ~~~ // 定義了一個字面量類型的 組件模板對象 export const m1 = { data() { return { sonmsg: {name: '小頭兒子', age: 6} } }, mounted() { alert('mixins提供的mounted生命函數') // this.info() }, methods: { info() { alert("mixins的info方法"); } } } ~~~ testximns ~~~ <template> <div class="hello"> {{sonmsg.age+10}} </div> </template> <script> import {m1} from '@/util/mixins-test' export default { name: "testmixins", props: { msg: String }, mixins: [m1], mounted() { alert("test自己的mounted方法"); // this.info() }, methods:{ info() { alert("test自己的info方法"); } } }; </script> ~~~ mounted方法執行了兩次 ![](https://img.kancloud.cn/8e/b0/8eb08270f738227e7b4af074ff5a9554_586x176.png) ![](https://img.kancloud.cn/68/e2/68e232a0184af7949d71ed03c3b666dc_710x201.png) ![](https://img.kancloud.cn/cd/6f/cd6f7c7eae10a25914c74a13586b97d0_649x201.png) 數據是隔離的 ![](https://img.kancloud.cn/41/89/41899c586ad87ae9598328b046210eb5_665x402.png) #### methods的方法會覆蓋mixins的方法 ~~~ mounted() { // alert("test自己的mounted方法"); this.info() }, ~~~ ~~~ mounted() { // alert('mixins提供的mounted生命函數') this.info() }, ~~~ 兩次mounted都執行自己的info方法 ![](https://img.kancloud.cn/01/cf/01cfb1f8b5f346cabe1f51e1503f7369_594x221.png) ![](https://img.kancloud.cn/a0/16/a016918f48d3981a8fb66ac2ec25e7fa_793x243.png)
                  <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>

                              哎呀哎呀视频在线观看