框架 -> 全方位、功能齊全javascript
代碼上的不一樣:css
通常使用框架:其框架在幫咱們運行已編寫好的代碼html
庫:小而精框架:大而全 ( 框架包含了各類庫 )vue
引包java
<script>
引入npm install --yes npm install vue --save ( npm install vue -S )
建立實例化對象 new Vue( options 對象 ) react
optionsnpm
data : 數據屬性 ( 既能夠是一個對象,也能夠是一個函數 )後端
template : 模板內容設計模式
vue的模板語法:app
{{ 表達式 }}
設計模式:MVC 與 MVVM
MVC:Model( 數據模型 )、View( 視圖 )、Controller( 控制器 )
MVVM:Model、View、ViewModel
在vue中提供了一些對於頁面+數據的更爲方便的輸出,這些操做就叫作指令
<div v-xxx></div>
經常使用指令
display:none;
,是基於css樣式的切換。v-if 與 v-show 的區別 ( 官網解釋 )
v-if
是「真正」的條件渲染,由於它會確保在切換過程當中條件塊內的事件監聽器和子組件適當的被銷燬和重建。v-if
也是惰性的:若是在初始渲染時條件爲假,則什麼也不作——直到條件第一次變爲真時,纔會開始渲染條件塊。v-show
就簡單的多——無論初始條件是什麼,元素總會被渲染。而且只是簡單的基於css進行 顯/隱 切換。v-if
有更高的切換開銷,而v-show
有更高的初始渲染開銷。所以,若是須要很是頻繁的切換,則使用v-show
較好;若是在運行時條件不多改變,則使用v-if
較好。v-bind:class="box"
,簡寫 :
。<div class="container" :class="{active: true}"></div> // 渲染爲 DOM <div class="container active"></div>
v-on:原生事件名 = '函數名'
,簡寫 @
// html <div class="container" @click="clickHandler"></div> // js methods:{ clickHandler(e){ console.log(this); } }
v-for="item,index in arr"
<ul> <li v-for="item,index in list"> {{index}}--{{item}} </li> </ul>
v-model:數據雙向綁定
<input v-model="msg"> // 等同於 <input :value="msg" @input="msg = $event.target.value" />
那麼v-model
其實就是v-bind
和v-on
的語法糖。
局部組件使用打油詩:聲子掛子用子
聲明子組件;父組件掛載子組件;父組件使用子組件。
// 局部組件的聲明 var App = { data(){ return{ } }, template:` <div>我是入口組件</div> ` } // vue實例 new Vue({ el:"", data(){ }, // 掛載子組件 components:{ App }, // 父組件直接可使用 template:`<App />` })
組件命名: 1.短橫線分隔 命名:當引用組件時,也要使用 短橫線分隔 形式。( 如: <my-compnent-name> )
2.駝峯式命名:當引用組件時,可使用 短橫線分隔 形式或者 駝峯 形式。( 如: <my-compnent-name> 或 <MyCompnentName>)
[注]:直接在DOM( 即非字符串的模板 )中使用時只有 短橫線分隔 形式是有效的。
[建議]:命名時用 駝峯 形式,使用時用 短橫線分隔 形式。
Vue.component("組件名", options)
// 全局組件聲明 // 第一個參數是組件的名字,第二個參數是options配置項 Vue.component("Vbtn", { template:` <button>按鈕</button> ` }) // 全局組件使用 var App = { template:` <div> ... <Vbtn /> </div> ` }
Vue.component("Parent", { data(){ return{ msg:"我是父組件的數據" } }, template:` <div> <p>我是父組件</p> <Child :childData="msg" /> </div> ` })
Vue.component("Child", { template:` <div> <p>我是子組件</p> <input type="text" v-model="childData" /> </div> `, props:["childData"] })
在子組件中 觸發原生的事件,在函數中使用 $emit
觸發其父組件中自定義的事件
$emit("自定義的事件名", "消息")
// Child Vue.component("Child", { template:` <div> <p>我是子組件</p> <input type="text" v-model="childData" @input="changeValue(childData)" /> </div> `, props:["childData"], methods:{ changeValue(val){ // 自定義的事件必定要經過 this.$emit() 去觸發 // $emit("自定義的事件名", "消息") this.$emit(childHandler, val) } } }) // Parent Vue.component("Parent", { data(){ return{ msg:"我是父組件的數據" } }, template:` <div> <p>我是父組件</p> <Child :childData="msg" @childHandler="childHandlerFn" /> </div> `, methods:{ childHandlerFn(val){ console.log(val); } } })
插槽:內置組件 slot,做爲承載分發內容的出口
/* 模擬 elementUI 按鈕組件的實現 */ // 子組件 Vue.component("Vbtn", { template:` <button class="default" :class="type"> <slot>按鈕</slot> </button> `, props:["type"] }) // 父組件 var App = { template:` <div> ... <Vbtn type="primary">登錄</Vbtn> <Vbtn type="success">註冊</Vbtn> </div> ` }
// 子組件 Vue.component("liItem", { template:` <li> 第一個槽 <slot name="idx-1"></slot> 第二個槽 <slot name="idx-2"></slot> </li> ` }) // 父組件 var App = { template:` <div> <ul> <liItem> <h1 slot="idx-1">我是第一個槽</h1> <h3 slot="idx-2">我是第二個槽</h3> </liItem> </ul> </div> ` }
分類
/* 局部過濾器 */ // 1.聲明過濾器 // 2. {{ 數據 | 過濾器的名字 }} var App = { data(){ return{ price:0, msg:"hello filter" } }, template:` <div> <input type="number" v-model="price" /> <!-- 使用過濾器 --> <h2>{{ price | myCurPrice }}</h2> <h3>{{ price | myReverse }}</h3> </div> `, filters:{ // 聲明過濾器 myCurPrice: function(val){ return "¥" + val } } } /* 全局過濾器 */ // 字符串反轉過濾器 Vue.filter("myReverse", function(val){ return val.split("").reverse().join(""); })
{{ 數據 | 過濾器的名字(能夠傳值) }}
例: {{ price | myReverse("這裏是傳入的值") }}
// 其中 arg 就是傳入的值
Vue.filter("myReverse", function(val, arg){
return val.split("").reverse().join("");
})
watch 監聽的是單個屬性基本的數據類型 -- 簡單監視
複雜的數據類型 -- 深度監視
new Vue({ el:"", data(){ return{ msg : '' } }, watch:{ msg: function(newVal, oldVal){ console.log(newVal, oldVal); } } })
new Vue({ el:"", data(){ return{ userList : [{name:"jack"}] } }, watch:{ userList: { deep:true, // 深度監視 handler: function(newVal, oldVal){ console.log(newVal, oldVal); } } } })
// html <img :src="getCurSrc" /> <ul> <li v-for="item,index in dataList" @click="clickHandler(index)" :class="{active: curIdx == index}">{{ index }}</li> </ul> // js new Vue({ el:"", data(){ return{ dataList:[{imgsrc:'1'},{imgsrc:'2'},{imgsrc:'3'}], curIdx : 0 } }, computed:{ // 計算屬性默認只有 getter getCurSrc: function(){ return this.dataList[this.curIdx].imgsrc } }, methods:{ clickHandler(index){ this.curIdx = index; } } })
// js new Vue({ computed:{ getCurSrc: { set: function(newVal){ console.log(newVal); this.curIdx = newVal; }, get: function(){ return this.dataList[this.curIdx].imgsrc } } }, methods:{ clickHandler(index){ // 點語法 set方法 和 get方法 // getter 方法 console.log(this.getCurSrc); // 調用set方法 this.getCurSrc = index; } } })
created:組件建立以後 會調用
mounted:掛載數據到 DOM 以後會調用
beforeUpdate:在更新DOM以前 調用該鉤子
updated:在更新DOM以後 調用該鉤子
vue 內置組件 <keep-alive></keep-alive>
:能在組件的切換過程當中將狀態保留在內存中,防止重複渲染DOM。