vue3.0-基本特性和用法

  1. 路由配置
    vue3.0使用createRouter來創(chuàng)建Vue Router實(shí)例
    之前用構(gòu)造函數(shù)的方式
createRouter, createWebHashHistory
const router = createRouter({
  history: createWebHashHistory(),
  [
{
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('../views/About.vue')
  }
  ]
})
export default router
  1. 初始化狀態(tài)
    vue3.0通過setup方法初始化狀態(tài)
    調(diào)用ref()方法返回一個(gè)響應(yīng)式的對(duì)象
    在setup中對(duì)ref返回的數(shù)值進(jìn)行操作時(shí),需要通過.value來獲取
// value1只有一個(gè)value屬性
setup() {
    let value1 = ref('disk')
    let ienter = () => {
      console.log(value.value);
    }
    let addValue1 = () => {
      value1.value ++
    }
    return {
      value, ienter,addValue1
    }
  }
  1. 計(jì)算屬性computed方法和監(jiān)聽器watch方法
    watch方法包含兩個(gè)參數(shù),兩個(gè)參數(shù)都是function
import { ref, toRefs, computed, watch } from 'vue'

setup() {
    let value1 = ref('disk')
    let ienter = () => {
      console.log(value.value);
    }
    let addValue1 = () => {
      value1.value ++
    }
    let douValue1 = computed(() => value1.value * 2)
    watch(() => value1.value, val=>{
      console.log(val)
    })
    return {
      value, ienter,addValue1, douValue1 
    }
  }
  1. 獲取路由
    vue3.0通過getCurrentInstance方法獲取當(dāng)前組件的實(shí)例
    通過getCurrentInstance方法的ctx屬性獲得當(dāng)前上下文
    通過計(jì)算computed獲取store.state.name的值
import { ref, toRefs, computed, watch, getCurrentInstance } from 'vue'

setup() {
    let value1 = ref('disk')
    let ienter = () => {
      console.log(value.value);
    }
    let addValue1 = () => {
      value1.value ++
    }
    let douValue1 = computed(() => value1.value * 2)
    watch(() => value1.value, val=>{
      console.log(val)
    })
    const { ctx } = getCurrentInstance()
    // 當(dāng)前路由信息
    // ctx.$router.currentRoute.value 
    
    return {
      value, ienter,addValue1, douValue1 
    }
  }
  1. Vuex使用
import Vuex from 'vuex'
export default Vuex.createStore({
  state: {
    name: 'licky'
  },
  mutations: {
    setName(state, payload) {
      state.test.a = payload
    }
  },
  actions: {
  },
  modules: {
  }
})

setup() {
    let value1 = ref('disk')
    let ienter = () => {
      console.log(value.value);
    }
    let addValue1 = () => {
      value1.value ++
    }
    let douValue1 = computed(() => value1.value * 2)
    watch(() => value1.value, val=>{
      console.log(val)
    })
    const { ctx } = getCurrentInstance()
    // 當(dāng)前路由信息
    // ctx.$router.currentRoute.value 
    
    let name = computed(() => ctx.$store.state.name)

    let editName = () => {
      ctx.$store.commit('setName', 'new Name zzz')
    }

    return {
      value, ienter,addValue1, douValue1, name, editName 
    }
  }

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容