<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之旅 廣告
                # 13 讓我們使用簡寫 > 原文: [https://javabeginnerstutorial.com/vue-js/13-shorthands-for-v-bind-and-v-on/](https://javabeginnerstutorial.com/vue-js/13-shorthands-for-v-bind-and-v-on/) 歡迎回來! 有人說簡寫嗎? 是的,這就是我們今天要關注的重點。 我們已經使用 [Vue 指令](https://javabeginnerstutorial.com/js/vue-js/what-is-vuejs/)已有一段時間了。 `v-`前綴有多種幫助。 它直觀地表示我們正在處理代碼中與 Vue 相關的屬性(最重要的原因)。 到目前為止,您應該已經了解`v-bind`和`v-on`是我們模板中最常用的兩個指令。 為什么? 因為我們一直在處理[事件](https://javabeginnerstutorial.com/vue-js/11-listening-to-dom-events-and-event-modifiers/)(特別是單擊)和[數據綁定](https://javabeginnerstutorial.com/vue-js/6-data-binding-p2/)! 因此,對于這兩個最常用的指令,Vue 為我們提供了捷徑或編寫它們的簡便方法。 ## 起始代碼 `Index.html` ```js <!DOCTYPE html> <html> <head> <title>Hello Vue!</title> <!-- including Vue with development version CDN --> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> </head> <body> <div id="app"> <h2>Welcome</h2> </div> <!-- including index.js file --> <script src="index.js"></script> </body> </html> ``` `Index.js` ```js new Vue({ el: "#app", data: { }, // define all custom methods within the 'methods' object methods: { } }); ``` ## `v-bind`的簡寫 讓我們使用`v-bind`屬性將 HTML `<a>`標記的`href`屬性綁定到“`https://www.google.com`” URL, *`Index.html`(代碼段)* ```js <a v-bind:href="url" target="_blank">Google</a> ``` 然后在 Vue 實例的數據對象中定義“`url`”, *`Index.js`(代碼段)* ```js data: { url: "https://www.google.com" } ``` 這段代碼工作得很好。 單擊鏈接“`Google`”,將打開一個新標簽,并導航到 Google 頁面。 但是我們想看起來很酷。 不是嗎?因此,編寫`v-bind`指令的簡短方法是一次性刪除`v-bind`一詞,而僅使用**冒號**。 ```js <!— Cool way of writing v-bind --> <a :href="url" target="_blank">Google</a> ``` 剛開始時可能看起來有些混亂,但是一旦您掌握了它,您很快就會感到贊賞。 這兩個代碼段(帶和不帶簡寫)的工作原理完全相同。 區別只是少了一些字符和更易讀的代碼。 如下圖所示,即使是簡寫形式,所有受 Vue.js 支持的瀏覽器(在我們的示例中為 Chrome)都可以正確解析它,并將`url`的值綁定到`href`屬性。 請注意,冒號(`v-bind`的簡寫語法)沒有出現在最終呈現的 HTML 中,可以在 Chrome DevTools 擴展塢的“元素”窗格中清楚地看到。 ![v-bind](https://img.kancloud.cn/3a/61/3a61a45995c3bde943a80f4daa3a4736_578x354.png) ## `v-on`的簡寫 為了理解`v-on`指令的簡寫語法,讓我們有一個按鈕。 單擊它后,我們將觸發名為“`greet`”的方法,以在消息`Hi`和`Hello`之間切換。 完整的語法是 *`Index.html`(代碼段)* ```html <button v-on:click="greet">Click for a different greeting</button> ``` *`Index.js`(代碼段)* ```jsscript data: { message: "Hi", url: "https://www.google.com" }, methods: { greet() { this.message === "Hi" ? this.message = "Hello" : this.message = "Hi"; } } ``` 好的。 此處的縮寫是將單詞`v-on`和冒號替換為`@`符號。 就這樣! 它是如此簡單!! *`Index.html`(代碼段)* ```html <!-- Using v-on shorthand --> <button @click="greet">Click for a different greeting</button> ``` ## 最終代碼 `Index.html` ```html <!DOCTYPE html> <html> <head> <title>Hello Vue!</title> <!-- including Vue with development version CDN --> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> </head> <body> <div id="app"> <h2>Welcome</h2> <!-- Using v-on shorthand --> <button @click="greet">Click for a different greeting</button> <p> {{ message }} </p> <!-- Using v-bind shorthand --> <a :href="url" target="_blank">Google</a> </div> <!-- including index.js file --> <script src="index.js"></script> </body> </html> ``` `Index.js` ```jsscript new Vue({ el: "#app", data: { message: "Hi", url: "https://www.google.com" }, // define all custom methods within the 'methods' object methods: { greet() { // 'this' keyword refers to the current Vue instance this.message === "Hi" ? this.message = "Hello" : this.message = "Hi"; } } }); ``` 代碼看起來更加簡潔,優雅且不那么冗長(顯然!)。 您將在處理事件負載的實際應用中更好地看到它。 請記住,使用簡寫語法完全是可選的,完全由您決定。 上面討論的所有代碼以及注釋都可以在 [GitHub 倉庫](https://github.com/JBTAdmin/vuejs)中找到。 *警告:我將在以后的所有代碼示例中使用這些簡寫形式。 與他們合作的次數越多,您就越感到舒適!* **為什么要使用簡寫?** 讓我們了解使用這些簡寫來說服您的原因, 1. 由于經常以完整形式使用這些指令,因此代碼變得冗長。 2. 在將 Vue.js 用作管理所有前端代碼的框架的應用(例如 [SPA](https://en.wikipedia.org/wiki/Single-page_application))中,很明顯正在使用 Vue,而前綴`v-`并不那么重要。 3. 干凈,易讀的代碼是每個開發人員最終想要的。 這使我們到了本文的結尾。 祝你今天愉快。
                  <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>

                              哎呀哎呀视频在线观看