Vue 2.0 基礎(chǔ)詳細(xì)
1、特點
是一個MVVM框架
由MVC架構(gòu)衍生,分為View(視圖層)、ViewModel(數(shù)據(jù)視圖層)、Model(數(shù)據(jù)層),MVVM 最標(biāo)志性的特性就是 數(shù)據(jù)綁定,實現(xiàn)數(shù)據(jù)驅(qū)動視圖,視圖同步數(shù)據(jù)。
數(shù)據(jù)也是單向的,稱之為單向數(shù)據(jù)流
數(shù)據(jù)總是從父組件傳遞到子組件中,子組件無權(quán)(不建議)直接修改父組件中的數(shù)據(jù)。
不兼容ie8及其以下瀏覽器
響應(yīng)式原理式利用了es5的Object.defineProperty,而該API不支持IE8
2、實例
// Vue單頁面實例
<template>
標(biāo)簽...
</template>
<script>
export default {
data () {},
methods: {},
computed: {},
watch: {}
}
</script>
<style>
樣式...
</style>
3、選項 Options
data
函數(shù),用于定義頁面的數(shù)據(jù)
data() {
return {
count: 2
copyCount: 1
}
}
// 使用
this.count // 2
computed
對象,計算屬性,用于簡化復(fù)雜邏輯處理
// 計算屬性不接受參數(shù),會緩存依賴數(shù)據(jù),必須要有return
computed:{
doubleCount: function () {
return this.count *= 2
},
}
// 使用
this.doubleCount // 4
methods
對象,用于定義頁面的函數(shù)
methods: {
cLog(msg) {
console.log(msg)
}
}
// 使用
this.cLog('調(diào)用了函數(shù)') // 控制臺輸出:調(diào)用了函數(shù)
watch
對象,屬性偵聽,用來監(jiān)聽某數(shù)據(jù)的改變并做出對應(yīng)操作
watch: {
count(value, [oldValue]) {
// value:改變后的值
this.copyCount = value + 1
}
}
// 當(dāng)count發(fā)生改變時自動觸發(fā)
this.count = 2
this.copyCount // 3
components
對象,注冊組件到頁面
import UploadImg from 'xxxx'
components: { UploadImg }
// template
<upload-img>
4、基本語法
常用指令
v-html:渲染HTML
v-if:判斷語法,控制顯示/隱藏,通過是否渲染DOM來控制
v-show:控制顯示/隱藏,與v-if類似,但v-show是通過display屬性控制
v-model:雙向數(shù)據(jù)綁定,一般用于表單,默認(rèn)綁定value屬性
v-bind:
- 簡寫 :class
- 用于動態(tài)屬性綁定
v-on:
- 簡寫 @click
- 用于事件綁定
v-for:遍歷語法,支持?jǐn)?shù)組、對象及字符串
5、生命周期
beforeCreated:創(chuàng)建Vue對象
created:data初始化,此時可以對實例做些預(yù)操作
beforeMounted:el和data初始化
mounted:掛載el和data,此時可以調(diào)用http請求
beforeUpdated:更新DOM前,此時可以進(jìn)一步地更改狀態(tài),不會觸發(fā)重渲染過程
updated:更新修改后的虛擬DOM到頁面,此時應(yīng)避免改動操作,以免造成無限循環(huán)更新
beforeDestory:頁面銷毀前,此時可以做一些重置操作,比如清除定時器 和 dom事件等
destoryed:頁面銷毀,此時Vue實例已被刪除,所有操作均無效
6、路由管理Vue-Router
官方的路由管理器。它和 Vue.js 的核心深度集成,讓構(gòu)建單頁面應(yīng)用變得易如反掌。
6.1 路由配置
// NPM下載
npm install vue-router
// router.js
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
// 定義頁面路由(路徑、組件)
export default new Router({
{ path: '/foo', component: () => import('/pages/foo') }, // 路由組件懶加載
{ path: '/bar', component: '/pages/bar'}
})
// main.js
import router from './router.js'
new Vue({
el: '#app',
router, // 掛載路由到Vue實例
render: h => h(App)
})
// page.vue
<!-- 要重點區(qū)分開兩者的關(guān)系 -->
this.$router // 訪問路由器,內(nèi)置push、replace的路由方法
this.$route // 訪問當(dāng)前路由,內(nèi)置path、query等路由屬性
// app.vue
<!-- 路由匹配到的組件將渲染在這里 -->
<router-view></router-view>
6.2 路由跳轉(zhuǎn)
申明式路由
<router-link :to="..."> <router-link :to="..." replace>
編程式路由
this.$router.push({ path: 'register', query: { plan: 'private' }})
this.$router.replace(...) // 與push區(qū)別在不插入history記錄
this.$router.go( [Number] n ) // 在 history 記錄中向前或者后退多少步
// 路由傳參攜帶中文時建議先使用encodeURLComponent轉(zhuǎn)碼,以免顯示亂碼。
6.3 路由守衛(wèi)
全局守衛(wèi)
對配置的所有路由均生效,但優(yōu)先級低與內(nèi)部路由。
全局前置守衛(wèi)(常用)
// 用戶未能驗證身份時重定向到 /login
router.beforeEach((to, from, next) => {
// to 即將要進(jìn)入的路由對象,from 來源路由,next 鉤子函數(shù),是否放行
if (to.name !== 'Login' && !isAuthenticated) next({ name: 'Login' })
else next()
})
全局解析守衛(wèi)(了解)
router.beforeResolve((to, from, next) => {
// ...
})
全局后置鉤子(了解)
router.afterEach((to, from) => {
// 該路由守衛(wèi)不接受 next 鉤子函數(shù)
// ...
})
路由獨享守衛(wèi)(了解)
該守衛(wèi)僅對配置的路由生效,這些守衛(wèi)與全局前置守衛(wèi)的方法參數(shù)是一樣的。
const router = new VueRouter({
routes: [
{
path: '/foo',
component: Foo,
beforeEnter: (to, from, next) => {
// ...
}
}
]
})
組件內(nèi)部守衛(wèi)(了解)
可以在路由組件內(nèi)直接定義以下路由導(dǎo)航守衛(wèi),僅對當(dāng)前組件生效。
beforeRouteEnter beforeRouteUpdate (2.2 新增) beforeRouteLeave
組件內(nèi)的守衛(wèi) | Vue-Router官方文檔
7、狀態(tài)管理器Vuex
7.1 配置
// store.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
...
export default new Vuex.Store({
state,
getters,
mutations,
actions,
modules
})
// main.js
import store from './store'
Vue.prototype.$store = store
8、五大核心屬性
state
數(shù)據(jù)源,不要直接修改state狀態(tài)
// store.js
const state = {
name: 'zzz'
}
<!--page.vue-->
// 1.直接調(diào)用
this.$store.state.name // 'zzz'
// 2.輔助函數(shù)映射
computed: {
...mapState(['name'])
}
this.name // 'zzz'
mutations
改變state狀態(tài)的唯一途徑
const mutations = {
updateName(state, newName) {
state.name = newName
}
}
<!--page.vue-->
// 1.直接調(diào)用
this.$store.commit(updateName, 'zzh') // state.name: 'zzh'
// 2.輔助函數(shù)映射
methods: {
...mapMutations(['updateName'])
}
this.updateName('zzh') // state.name: 'zzh'
actions
存放異步操作,異步觸發(fā)mutation改變state
const actions = {
asyncUpdateName(context) {
// 異步操作,例如發(fā)起http請求去獲取更新后的name,假設(shè)name=xxx
...
context.commit('updateName', res.name) // state.name: 'xxx'
}
}
<!--page.vue-->
// 1.直接調(diào)用
this.$store.dispatch(asyncUpdateName)
// 2.輔助函數(shù)映射
methods: {
...mapActions(['asyncUpdateName'])
}
this.asyncUpdateName()
getters
數(shù)據(jù)源處理,類似計算屬性
const getter = {
formatName(state) {
return state.name + '2021'
}
}
<!--page.vue-->
// 1.直接調(diào)用
this.$store.getter.formatName() // 'xxx2021'
// 2.輔助函數(shù)映射
computed: {
...mapGetters(['formatName'])
}
this.formatName() // // 'xxx2021'
modules
模塊化,讓每個模塊單獨管理一套自己的state、mutations、actions和getters。
// 模塊內(nèi)部 this.$store.state.name // 內(nèi)部訪問無須使用命名空間 // 模塊外部 this.$store.state.login.name // login是模塊命名空間 ... 其他屬性類似
9、Http請求庫Axios
基于 promise 封裝的Http請求庫,官方推薦
<!-- api.js -->
import axios from 'axios'
// 創(chuàng)建并配置實例
axios.create({
baseURL: 'http://www.baidu.com', // 請求基準(zhǔn)地址
timeout: 1000 // 請求超時時間
...
})
// 請求攔截器
axios.interceptors.request.use(request => {
request.headers['Content-Type'] = 'application/json'
...
})
// 響應(yīng)攔截器
axios.interceptors.response.use(response => {
...
return response.data
})
// 配置請求
export default {
getId: () => axios.get('/getId'),
getNameById: id => axios.post('/getNameById', id)
}
<!-- main.js -->
import api from './api.js'
Vue.prototype.$api = api
<!-- page.vue -->
this.$api.getId().then(res => {
...
}).catch()
到此這篇關(guān)于Vue 2.0 基礎(chǔ)詳細(xì)的文章就介紹到這了,更多相關(guān)Vue 2.0 基礎(chǔ)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue中如何實現(xiàn)后臺管理系統(tǒng)的權(quán)限控制的方法示例
這篇文章主要介紹了vue中如何實現(xiàn)后臺管理系統(tǒng)的權(quán)限控制的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09
深入解析Vue源碼實例掛載與編譯流程實現(xiàn)思路詳解
這篇文章主要介紹了Vue源碼實例掛載與編譯流程實現(xiàn)思路詳解,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05
VUE+elementui組件在table-cell單元格中繪制微型echarts圖
這篇文章主要介紹了VUE+elementui組件在table-cell單元格中繪制微型echarts圖,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Element的穿梭框數(shù)據(jù)量大時點擊全選卡頓的解決方案
本文主要介紹了Element的穿梭框數(shù)據(jù)量大時點擊全選卡頓的解決方案,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10
Vue中使用js制作進(jìn)度條式數(shù)據(jù)對比動畫
這篇文章主要為大家詳細(xì)介紹了Vue中使用js制作進(jìn)度條式數(shù)據(jù)對比動畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03

