zl程序教程

您现在的位置是:首页 >  其它

当前栏目

(二)一个不用VueRouter的例子

一个 例子 不用
2023-06-13 09:17:09 时间
<template>
  <nav>
    <a
      v-for="(route, path) in routes"
      :href="path"
      @click.prevent="changeRoute(path)"
      >{{ route.label }}</a
    >
  </nav>
  <component :is="currentPage" />
</template>

<script setup>
import PageOne from "./components/PageOne.vue";
import PageTwo from "./components/PageTwo.vue";
import PageThree from "./components/PageThree.vue";

import { ref, computed } from "vue";

const routes = {
  "/": {
    component: PageOne,
    label: "页面1",
  },
  "/2": {
    component: PageTwo,
    label: "页面2",
  },
  "/3": {
    component: PageThree,
    label: "页面3",
  },
};

const currentPath = ref(location.pathname);

const currentPage = computed(
  () => routes[currentPath.value].component || PageOne
);

function changeRoute(path) {
    // 使用浏览器自带的api 来实现
  history.pushState(null, null, path);
  currentPath.value = location.pathname;
}
</script>

总结:写在最后

  • 对于小型应用可以使用这样的方式来实现,大型应用还是使用 VueRouter