強大Vue.js元件詳細說明
這篇文章主要為大家講述強大Vue.js元件詳細說明,元件是Vue.js最強大的功能之一,有興趣的夥伴們可以參考一下
什麼是元件:元件是Vue.js最強大的功能之一。元件可以擴充HTML元素,封裝可重複使用的程式碼。在較高層面上,元件是自定義的元素,Vue.js的編譯器為它添加特殊功能。在某些情況下,元件也可以是原生HTML元素的形式,以is特性擴充。
如何註冊元件?
需要使用Vue.extend方法建立一個元件,然後使用Vue.component方法註冊元件。 Vue.extend方法格式如下:
var MyComponent = Vue.extend({ // 选项...后面再介绍 })
如果你想要在其他地方使用這個建立的元件,就得個元件命個名稱:
Vue.component('my-component ', MyComponent)
命名之後即可在HTML標籤中使用這個元件名稱,像使用DOM元素一樣。下面來看看一個完整的組件註冊和使用範例。
html程式碼:
<p id="example"> <my-component></my-component> </p>
js程式碼:
// 定义 var MyComponent = Vue.extend({ template: '<p>A custom component!</p>' }) // 注册 Vue.component('my-component', MyComponent) // 创建根实例 new Vue({ el: '#example' })
輸出結果:
<p id="example"> <p>A custom component!</p> </p
##巢狀元件
元件本身也可以包含元件,而下面的parent元件就包含了一個命名為child-component元件,但這個元件只能被parent元件使用:
var child = Vue.extend({ template: '<p>A custom component!</p>' }); var parent = Vue.extend({ template: '<p>Parent Component: <child-component></child-component></p>', components: { 'child-component': child } }); Vue.component("parent-component", parent);
// 在一个步骤中扩展与注册 Vue.component('my-component', { template: '<p>A custom component!</p>' }) // 局部注册也可以这么做 var Parent = Vue.extend({ components: { 'my-component': { template: '<p>A custom component!</p>' } } })
動態元件
currentView動態切換元件顯示。
<p id="dynamic"> <button id="home">Home</button> <button id="posts">Posts</button> <button id="archive">Archive</button> <br> <component :is="currentView"></component> </p>
var vue = new Vue({ el:"#dynamic", data: { currentView: "home" }, components: { home:{ template: "Home" }, posts: { template: "Posts" }, archive: { template: "Archive" } } }); document.getElementById("home").onclick = function(){ vue.currentView = "home"; }; document.getElementById("posts").onclick = function(){ vue.currentView = "posts"; }; document.getElementById("archive").onclick = function(){ vue.currentView = "archive"; };
:item="item"
:index="$ index">
不自動把item 注入元件的原因是這會導致元件跟目前v-for 緊密耦合。明確聲明資料來自哪裡可以讓元件重複使用在
其它地方。
深入響應式原理
物件傳給vue實例作為data的選項,vue.js將遍歷它的屬性,用Object. defineProperty將它轉換為getter/setter。這是ES5特性,所有vue.js不支援IE8或更低版本。
資料綁定都有一個對應的watcher對象,在計算過程中它把屬性記錄為依賴。之後當依賴的setter被呼叫時 ,會觸發watcher重新計算。流程如下所示:
刪除,屬性必須在data上才能讓vue.js轉換它為getter/setter模式,才能有回應。例如:
var data = { a: 1 }; var vm = new Vue({ data: data }); // `vm.a` 和 `data.a` 现在是响应的 vm.b = 2 // `vm.b` 不是响应的 data.b = 2 // `data.b` 不是响应的
key,value)實例方法:
// `vm.b` 和`data. b` 現在是回應的
对于普通对象可以使用全局方法:Vue.set(object, key, value):
Vue.set(data, 'c', 3)
// `vm.c` 和 `data.c` 现在是响应的
初始化数据:尽管Vue.js提供动态的添加相应属性,还是推荐在data对象上声明所有的相应属性。
不这么做:
var vm = new Vue({ template: '<p>{{msg}}</p>' }) // 然后添加 `msg` vm.$set('msg', 'Hello!')
应该这么做:
var vm = new Vue({ data: { // 以一个空值声明 `msg` msg: '' }, template: '<p>{{msg}}</p>' }) // 然后设置 `msg` vm.msg = 'Hello!'
组件完整案例
下面介绍的例子实现了模态窗口功能,代码也比较简单。
html代码:
<!-- 实现script定义一个模板 --> <script type="x/template" id="modal-template"> <!--模板是否显示通过v-show="show"来设置, transition设置动画效果--> <p class="modal-mask" v-show="show" transition="modal"> <p class="modal-wrapper"> <p class="modal-container"> <p class="modal-header"> <!--slot 相当于header占位符--> <slot name="header"> default header </slot> </p> <p class="modal-body"> <!--slot 相当于body占位符--> <slot name="body"> default body </slot> </p> <p class="modal-footer"> <!--slot 相当于footer占位符--> <slot name="footer"> default footer </slot> <button class="modal-default-button" @click="show = false">OK</button> </p> </p> </p> </p> </script> <p id="app"> <!--点击按钮时设置vue实例特性showModal的值为true--> <button id="show-modal" @click="showModal = true">show modal</button> <!--modal是自定义的一个插件,插件的特性show绑定vue实例的showModal特性--> <modal :show.sync="showModal"> <!--替换modal插件中slot那么为header的内容--> <h3 id="Custom-nbsp-Header">Custom Header</h3> </modal> </p>
js代码:
//定义一个插件,名称为modal Vue.component("modal", { //插件的模板绑定id为modal-template的DOM元素内容 template: "#modal-template", props: { //特性,类型为布尔 show:{ type: Boolean, required: true, twoWay: true } } }); //实例化vue,作用域在id为app元素下, new Vue({ el: "#app", data: { //特性,默认值为false showModal: false } });
css代码:
.modal-mask { position: fixed; z-index: 9998; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, .5); display: table; transition: opacity .3s ease; } .modal-wrapper { display: table-cell; vertical-align: middle; } .modal-container { width: 300px; margin: 0px auto; padding: 20px 30px; background-color: #fff; border-radius: 2px; box-shadow: 0 2px 8px rgba(0, 0, 0, .33); transition: all .3s ease; font-family: Helvetica, Arial, sans-serif; } .modal-header h3 { margin-top: 0; color: #42b983; } .modal-body { margin: 20px 0; } .modal-default-button { float: right; } /* * the following styles are auto-applied to elements with * v-transition="modal" when their visiblity is toggled * by Vue.js. * * You can easily play with the modal transition by editing * these styles. */ .modal-enter, .modal-leave { opacity: 0; } .modal-enter .modal-container, .modal-leave .modal-container { -webkit-transform: scale(1.1); transform: scale(1.1); }
相关文章:
以上是強大Vue.js元件詳細說明的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

不少用戶在玩win10的的一些遊戲的時候總是會遇到一些問題,比如說卡屏和花屏等等情況,這個時候我們是可以採用打開directplay這個功能來解決的,而且功能的操作方法也很簡單。 win10舊版元件directplay怎麼安裝1、在搜尋框裡面輸入「控制台」然後開啟2、檢視方式選擇大圖示3、找到「程式與功能」4、點選左側的啟用或關閉win功能5、選擇舊版這裡的勾選上就可以了

Ace 是一個用 JavaScript 寫的可嵌入程式碼編輯器。它與 Sublime、Vim 和 TextMate 等原生編輯器的功能和效能相符。它可以很容易地嵌入到任何網頁和 JavaScript 應用程式中。 Ace 被維護為Cloud9 IDE的主要編輯器 ,並且是 Mozilla Skywriter (Bespin) 專案的繼承者。

在當今前端開發中,Vue.js 已經成為了一個非常流行的框架。隨著 Vue.js 的不斷發展,單元測試變得越來越重要。今天,我們將探討如何在 Vue.js 3 中編寫單元測試,並提供一些最佳實踐和常見的問題及解決方案。

Vue是一款非常流行的前端框架,它提供了許多工具和功能,如元件化、資料綁定、事件處理等,能夠幫助開發者建立出高效、靈活和易於維護的Web應用程式。在這篇文章中,我來介紹如何使用Vue實作一個日曆元件。 1.需求分析首先,我們需要分析這個行事曆組件的需求。一個基本的日曆應該具備以下功能:展示當前月份的日曆頁面;支援切換到前一月或下一月;支援點擊某一天,

Vue是目前最受歡迎的前端框架之一,而VUE3則是Vue框架的最新版本,相較於VUE2,VUE3具備了更高的性能和更出色的開發體驗,成為了眾多開發者的首選。在VUE3中,使用extends繼承元件是一個非常實用的開發方式,本文將為大家介紹如何使用extends繼承元件。 extends是什麼?在Vue中,extends是一個非常實用的屬性,它可以用於子元件繼承父

在做 chatgpt 鏡像站的時候,發現有些鏡像站是沒做打字機的遊標效果的,就只是文字輸出,是他們不想做嗎?反正我想做。於是我仔細研究了一下,實現了打字機效果加遊標的效果,現在分享一下我的解決方案以及效果圖~

Angular框架中元件的預設顯示行為不是區塊級元素。這種設計選擇促進了元件樣式的封裝,並鼓勵開發人員有意識地定義每個元件的顯示方式。透過明確設定CSS屬性 display,Angular組件的顯示可以完全控制,從而實現所需的佈局和響應能力。

win10舊版元件是需要使用者自己去設定裡面打開的,因為很多的元件平時都是預設關閉的狀態,首先我們需要進入到設定裡面,操作很簡單,跟著下面的步驟來就可以了win10舊版元件在哪裡開啟1、點選開始,然後點選「win系統」2、點選進入控制台3、再點選下面的程式4、點選「啟用或關閉win功能」5、在這裡就可以選擇你要的開啟了
