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