VUE3-学习:路由

代码示例: 

<script setup>
// This starter template is using Vue 3 <script setup> SFCs
// Check out https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup
import HomeVue from './components/Home.vue';
import AboutVue from './components/About.vue';
import { ref, computed } from 'vue'
const routes = {
  '/': HomeVue,
  '/about': AboutVue
}
const currentPath = ref(window.location.hash)

window.addEventListener('hashchange', () => {
	currentPath.value = window.location.hash
})
const currentView = computed(() => {
    return routes[currentPath.value.slice(1)] || '/' || NotFound
})

</script>

<template>
   <a href="#/">Home</a> |
   <a href="#/about">About</a> |
   <a href="#/non-existent-path">Broken Link</a>
   <component :is="currentView" />
</template>

学习:

  1. 页面是通过computed()实现更新页面,同样也可以是ref等响应式更新;
  2. 而触发更新事件是由当前页面URL改变,使用的是hashchange;
  3. href="#/" 中#是锚点指向当前页面;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值