欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue 2.0 基礎(chǔ)詳細(xì)

 更新時(shí)間:2021年10月21日 10:01:57   作者:吳知木  
這篇文章主要介紹了Vue 2.0 基礎(chǔ),具體內(nèi)容有、基本語法、生命周期、路由管理Vue-Router、狀態(tài)管理Vuex、Http請(qǐng)求庫Axios,想詳細(xì)了解的小伙伴請(qǐng)和現(xiàn)編一起學(xué)習(xí)下面文章內(nèi)容吧

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ù)也是單向的,稱之為單向數(shù)據(jù)流

數(shù)據(jù)總是從父組件傳遞到子組件中,子組件無權(quán)(不建議)直接修改父組件中的數(shù)據(jù)。

不兼容ie8及其以下瀏覽器

響應(yīng)式原理式利用了es5的Object.defineProperty,而該API不支持IE8

2、實(shí)例

// Vue單頁面實(shí)例
<template>
    標(biāo)簽...
</template>
<script>
    export default {
      data () {},
      methods: {},
      computed: {},
      watch: {}
    }
</script>
<style>
    樣式...
</style>

3、選項(xiàng) Options

data

函數(shù),用于定義頁面的數(shù)據(jù)

data() {
    return {
        count: 2
        copyCount: 1
    }
}

// 使用
this.count // 2

computed

對(duì)象,計(jì)算屬性,用于簡化復(fù)雜邏輯處理

// 計(jì)算屬性不接受參數(shù),會(huì)緩存依賴數(shù)據(jù),必須要有return
computed:{
    doubleCount: function () {
      return this.count *= 2
    },
}

// 使用
this.doubleCount // 4

methods

對(duì)象,用于定義頁面的函數(shù)

methods: {
    cLog(msg) {
        console.log(msg)
    }
}

// 使用
this.cLog('調(diào)用了函數(shù)') // 控制臺(tái)輸出:調(diào)用了函數(shù)

watch

對(duì)象,屬性偵聽,用來監(jiān)聽某數(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ì)象,注冊組件到頁面

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
  • 用于動(dòng)態(tài)屬性綁定

v-on

  • 簡寫 @click
  • 用于事件綁定

v-for:遍歷語法,支持?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ā)重渲染過程

updated:更新修改后的虛擬DOM到頁面,此時(shí)應(yīng)避免改動(dòng)操作,以免造成無限循環(huán)更新

beforeDestory:頁面銷毀前,此時(shí)可以做一些重置操作,比如清除定時(shí)器 和 dom事件等

destoryed:頁面銷毀,此時(shí)Vue實(shí)例已被刪除,所有操作均無效

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實(shí)例
  render: h => h(App)
})

// page.vue
<!-- 要重點(diǎn)區(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 記錄中向前或者后退多少步

// 路由傳參攜帶中文時(shí)建議先使用encodeURLComponent轉(zhuǎn)碼,以免顯示亂碼。

6.3 路由守衛(wèi)

全局守衛(wèi)

對(duì)配置的所有路由均生效,但優(yōu)先級(jí)低與內(nèi)部路由。

全局前置守衛(wèi)(常用)

// 用戶未能驗(yàn)證身份時(shí)重定向到 /login
router.beforeEach((to, from, next) => {
  // to 即將要進(jìn)入的路由對(duì)象,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ù)
  // ...
})

路由獨(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ù)源處理,類似計(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)部訪問無須使用命名空間
// 模塊外部
this.$store.state.login.name  // login是模塊命名空間
...
其他屬性類似

modules|Vuex官方文檔

9、Http請(qǐng)求庫Axios

基于 promise 封裝的Http請(qǐng)求庫,官方推薦

<!-- 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)文章

最新評(píng)論