中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁(yè) > news >正文

有什么網(wǎng)站可以免費(fèi)建站建網(wǎng)站教程

有什么網(wǎng)站可以免費(fèi)建站,建網(wǎng)站教程,石家莊做網(wǎng)站公司有哪些,企業(yè)網(wǎng)站用個(gè)人備案Vue 2 和 Vue 3 在路由封裝方面有一些區(qū)別,主要體現(xiàn)在 Vue Router 版本的升級(jí)(Vue Router 3 -> Vue Router 4)上。下面我們來(lái)對(duì)比一下 Vue 2 和 Vue 3 在路由封裝上的主要區(qū)別,并提供相應(yīng)的代碼示例。 1. Vue 2 路由封裝&#…

Vue 2 和 Vue 3 在路由封裝方面有一些區(qū)別,主要體現(xiàn)在 Vue Router 版本的升級(jí)(Vue Router 3 -> Vue Router 4)上。下面我們來(lái)對(duì)比一下 Vue 2 和 Vue 3 在路由封裝上的主要區(qū)別,并提供相應(yīng)的代碼示例。


1. Vue 2 路由封裝(基于 Vue Router 3)

Vue 2 使用 Vue.use(VueRouter) 注冊(cè)路由,并且 new VueRouter({}) 創(chuàng)建路由實(shí)例。

安裝 Vue Router 3

npm install vue-router@3

router/index.js(Vue 2 版)

import Vue from "vue";
import VueRouter from "vue-router";
import Home from "@/views/Home.vue";
import About from "@/views/About.vue";Vue.use(VueRouter);const routes = [{path: "/",name: "Home",component: Home,},{path: "/about",name: "About",component: About,},
];const router = new VueRouter({mode: "history",base: process.env.BASE_URL,routes,
});export default router;

main.js(Vue 2 版)

import Vue from "vue";
import App from "./App.vue";
import router from "./router";Vue.config.productionTip = false;new Vue({router, // 掛載路由render: (h) => h(App),
}).$mount("#app");

2. Vue 3 路由封裝(基于 Vue Router 4)

Vue 3 需要使用 createRoutercreateWebHistory 創(chuàng)建路由,并且 app.use(router) 掛載。

安裝 Vue Router 4

npm install vue-router@4

router/index.js(Vue 3 版)

import { createRouter, createWebHistory } from "vue-router";
import Home from "@/views/Home.vue";
import About from "@/views/About.vue";const routes = [{path: "/",name: "Home",component: Home,},{path: "/about",name: "About",component: About,},
];const router = createRouter({history: createWebHistory(),routes,
});export default router;

main.js(Vue 3 版)

import { createApp } from "vue";
import App from "./App.vue";
import router from "./router";const app = createApp(App);app.use(router); // 掛載路由
app.mount("#app");

3. Vue 2 和 Vue 3 路由封裝的主要區(qū)別

對(duì)比項(xiàng)Vue 2 (Vue Router 3)Vue 3 (Vue Router 4)
路由注冊(cè)Vue.use(VueRouter)createRouter() + app.use(router)
路由實(shí)例創(chuàng)建new VueRouter({})createRouter({})
路由模式mode: 'history' / mode: 'hash'history: createWebHistory() / createWebHashHistory()
router.beforeEach直接使用 router.beforeEach直接使用 router.beforeEach
this.$router組件內(nèi)部可用組件內(nèi)部可用
this.$route組件內(nèi)部可用組件內(nèi)部可用
router.push()this.$router.push('/about')this.$router.push('/about')

4. 路由守衛(wèi)封裝

Vue 2 全局前置守衛(wèi)

router.beforeEach((to, from, next) => {if (to.meta.requiresAuth && !isAuthenticated()) {next("/login");} else {next();}
});

Vue 3 全局前置守衛(wèi)(寫法一致)

router.beforeEach((to, from, next) => {if (to.meta.requiresAuth && !isAuthenticated()) {next("/login");} else {next();}
});

5. 動(dòng)態(tài)路由注冊(cè)

Vue 2 添加動(dòng)態(tài)路由

router.addRoutes([{path: "/dynamic",name: "Dynamic",component: () => import("@/views/Dynamic.vue"),},
]);

Vue 3 添加動(dòng)態(tài)路由(不同)

router.addRoute({path: "/dynamic",name: "Dynamic",component: () => import("@/views/Dynamic.vue"),
});

6. 路由懶加載

Vue 2 和 Vue 3 的路由懶加載寫法基本相同:

const routes = [{path: "/",name: "Home",component: () => import("@/views/Home.vue"),},
];

7. setup 中使用路由(Vue 3 專屬)

Vue 3 組合式 API 使用 useRouteruseRoute 訪問(wèn)路由:

import { useRouter, useRoute } from "vue-router";
import { onMounted } from "vue";export default {setup() {const router = useRouter();const route = useRoute();onMounted(() => {console.log("當(dāng)前路徑:", route.path);});const goToHome = () => {router.push("/");};return { goToHome };},
};

8. 結(jié)論

  • Vue 3 需要使用 createRouter,不再使用 Vue.use(VueRouter)。
  • Vue 3 需要 app.use(router) 掛載,而 Vue 2 在 new Vue({ router }) 中掛載。
  • Vue 3 組合式 API 可以用 useRouteruseRoute 獲取路由信息,Vue 2 仍使用 this.$routerthis.$route
  • addRoutes 改為 addRoute,但功能類似。

總體來(lái)說(shuō),Vue 3 使路由 API 變得更加模塊化,適配了 setup 語(yǔ)法,但大多數(shù)核心概念和 Vue 2 保持一致。

你是要封裝 Vue 3 版本的路由嗎?還是希望對(duì) Vue 2 的封裝進(jìn)行優(yōu)化?

http://www.risenshineclean.com/news/57057.html

相關(guān)文章:

  • 中山網(wǎng)站建設(shè)制作怎樣在百度上發(fā)布信息
  • 哪里網(wǎng)站建設(shè)公司好友鏈交易平臺(tái)
  • 服裝行業(yè)網(wǎng)站模板網(wǎng)絡(luò)推廣文案
  • xampp搭建wordpress長(zhǎng)沙優(yōu)化網(wǎng)站
  • 山東有哪些網(wǎng)絡(luò)公司優(yōu)化法治化營(yíng)商環(huán)境
  • 用dreamweaver怎么做網(wǎng)站百度推廣登錄賬號(hào)首頁(yè)
  • wordpress加中文字體山東搜索引擎優(yōu)化
  • 青海省建設(shè)廳官方網(wǎng)站建設(shè)云蘭州seo實(shí)戰(zhàn)優(yōu)化
  • 網(wǎng)站建設(shè)的主要工作西安seo陽(yáng)建
  • 男女直接做那個(gè)的視頻網(wǎng)站專業(yè)網(wǎng)站建設(shè)公司首選
  • 開鎖做網(wǎng)站怎么樣pc優(yōu)化工具
  • 金閶網(wǎng)站建設(shè)什么是精準(zhǔn)營(yíng)銷
  • 怎么做跟別人一樣的網(wǎng)站自助建站系統(tǒng)哪個(gè)好
  • nginx wordpress安全商丘seo公司
  • 做網(wǎng)站加盟企業(yè)如何做好網(wǎng)絡(luò)營(yíng)銷
  • 如何做婚戀網(wǎng)站北京網(wǎng)站優(yōu)化推廣方案
  • 長(zhǎng)沙景點(diǎn)一日游攻略西安seo代運(yùn)營(yíng)
  • 網(wǎng)站的概念百度seo和谷歌seo有什么區(qū)別
  • 建設(shè)農(nóng)產(chǎn)品網(wǎng)站總結(jié)ppt整站排名優(yōu)化公司
  • 現(xiàn)在學(xué)java的都是傻子 知乎win10優(yōu)化工具
  • 簡(jiǎn)單大氣的企業(yè)網(wǎng)站韓國(guó)搜索引擎排名
  • 直播型網(wǎng)站開發(fā)怎么在網(wǎng)上推廣廣告
  • 動(dòng)易學(xué)校網(wǎng)站管理系統(tǒng) 下載各大網(wǎng)站收錄提交入口
  • 電腦銷售網(wǎng)站開發(fā)論文企業(yè)網(wǎng)站seo推廣方案
  • 網(wǎng)站域名備案誰(shuí)來(lái)做太原seo顧問(wèn)
  • 做網(wǎng)站需要公司有哪些游戲優(yōu)化大師官網(wǎng)
  • 深圳市網(wǎng)站維護(hù)外貿(mào)谷歌優(yōu)化
  • 做網(wǎng)站掙錢快又多武漢seo招聘
  • 做網(wǎng)站的網(wǎng)絡(luò)公司有哪些linux網(wǎng)站入口
  • 網(wǎng)站建設(shè)公司前景如何在百度上營(yíng)銷