<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之旅 廣告
                [TOC] >[success] # Provide,Inject,模版 Ref 的用法 本章將講解 **Provide、Inject、Ref** 在 **composition API** 中如何使用,但是我們說的這個**Ref** ,是 **獲取dom元素** 的那個**Ref** ,不是 **定義基本類型響應式數據** 時用的那個 **Ref** 。 >[success] ## Provide Inject 的用法 **composition API** 解決了 **Provide Inject** 數據響應式的問題,下面看一下在 **composition API** 中如何使用 **Provide Inject** ,如下代碼: 1. **inject 默認值** **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Provide、Inject的用法</title> <!-- 通過cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> // 父組件 const app = Vue.createApp({ setup(){ // 引入 provide const { provide } = Vue; // 向【后代】傳遞數據(格式:provide(屬性名,值)) provide('name', 'dell') return { } }, template: ` <div> <child /> </div> ` }) // 子組件 app.component('child', { setup(){ // 引入 inject const { inject } = Vue; // 獲取【祖先】組件傳的數據(格式:inject('屬性名', '默認值'),如果 provide 沒有 name 就使用默認值小明) const name = inject('name', '小明') // 返回數據 return { name } }, template: '<div>{{ name }}</div>' }) // 掛載 const vm = app.mount('#root') </script> </html> ~~~ 上面代碼中 **inject** 如果獲取不到值,默認賦值為 **inject** 的第 **2** 個參數(**默認值** )。 2. **后代修改祖先值** **Vue** 是 **單向數據流** 的概念,**子組件** 不可以 **修改 父組件** 傳入的 **數據** 。那使用 **Provide、Inject** 時,**后代組件** 想修改 **祖先組件 Provide 傳過來的值** ,該怎么修改呢? 2.1 **錯誤示范** 下面這種寫法,雖然也可以修改數據,但是 **不符合單向數據流的要求** **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Provide、Inject的用法</title> <!-- 通過cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> // 父組件 const app = Vue.createApp({ setup(){ // 引入 provide const { provide, ref } = Vue; // 向【后代】傳遞數據 provide('name', ref('dell')) return { } }, template: ` <div> <child /> </div> ` }) // 子組件 app.component('child', { setup(){ // 引入 inject const { inject } = Vue; // 獲取【祖先】組件傳的數據 const name = inject('name') const handleClick = () => { name.value = '小明' } // 返回數據 return { name, handleClick } }, template: '<div @click="handleClick">{{ name }}</div>' }) // 掛載 const vm = app.mount('#root') </script> </html> ~~~ 2.2 **正確寫法** **后代組件需要調用祖先組件的方法,然后在祖先組件中去修改數據** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Provide、Inject的用法</title> <!-- 通過cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> // 父組件 const app = Vue.createApp({ setup(){ // 引入 provide const { provide, ref } = Vue; const name = ref('dell') // 向【后代】傳遞數據 provide('name', name) // 向【后代】傳遞 changeName 方法 provide('changeName', (value) => { name.value = value }) return { } }, template: ` <div> <child /> </div> ` }) // 子組件 app.component('child', { setup(){ // 引入 inject const { inject } = Vue; // 獲取【祖先】組件傳的數據 const name = inject('name') const changeName = inject('changeName') const handleClick = () => { changeName('小明') } // 返回數據 return { name, handleClick } }, template: '<div @click="handleClick">{{ name }}</div>' }) // 掛載 const vm = app.mount('#root') </script> </html> ~~~ 3. **搭配使用 readonly ,防止單向數據流被后代組件篡改** 有許多時候,許多人不懂得 **單向數據流的概念** ,還是會在 **后代組件** 中去修改 **祖先組件** 傳入的值,為了解決這個問題,可以使用 **readonly** 把參數變成只讀,來解決這個問題,代碼如下: **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Provide、Inject的用法</title> <!-- 通過cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> // 父組件 const app = Vue.createApp({ setup(){ // 引入 provide const { provide, ref, readonly } = Vue; const name = ref('dell') // 向【后代】傳遞數據 provide('name', readonly(name)) return { } }, template: ` <div> <child /> </div> ` }) // 子組件 app.component('child', { setup(){ // 引入 inject const { inject } = Vue; // 獲取【祖先】組件傳的數據 const name = inject('name') const handleClick = () => { name.value = '小明' } // 返回數據 return { name, handleClick } }, template: '<div @click="handleClick">{{ name }}</div>' }) // 掛載 const vm = app.mount('#root') </script> </html> ~~~ >[success] ## Ref 的用法 在 **composition API** 中,我們如何通過 **ref** 獲取 **dom** 元素呢? **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Ref 的用法</title> <!-- 通過cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> const app = Vue.createApp({ setup(){ const { onMounted, ref } = Vue; // 01. 名字與標簽 ref 的名字相等,這是固定寫法 const hello = ref(null) onMounted(() => { // 獲取 ref 有 hello 的 dom 元素 console.log(hello.value) }) // 02. 導出 hello return { hello } }, // 03. ref 起名為 hello template: ` <div ref="hello">小明</div> ` }) // 掛載 const vm = app.mount('#root') </script> </html> ~~~
                  <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>

                              哎呀哎呀视频在线观看