Vue路由基礎(chǔ)知識(shí)點(diǎn)

vue構(gòu)建

  import VueRouter from "vue-router";
  Vue.use(VueRouter);
  const routes = [{ path: "路徑",name: "名字",component:組件名}];
  const router = new VueRouter({routes,mode: "history"});//mode設(shè)置后路徑不會(huì)出現(xiàn)/*/
  new Vue({router,render: h => h(App)}).$mount("#app");

router-link實(shí)現(xiàn)跳轉(zhuǎn)

<router-link tag="li" class="nav-link" :to="{name:'HistaryLink'}">//router-link未設(shè)置tag時(shí)表示a標(biāo)簽,設(shè)置tag后表示為所設(shè)置的標(biāo)簽,所跳轉(zhuǎn)到頁(yè)面可用to直接表示,也可用:to用所設(shè)置name表示

$router事件跳轉(zhuǎn)

      //跳轉(zhuǎn)到上一次瀏覽的頁(yè)面
      this.$router.go(-1);
      //指定跳轉(zhuǎn)的地址
      this.$router.replace("/menu");
     // 指定跳轉(zhuǎn)到路由名字下
      this.$router.replace({ name: "menuLink" });
      //通過(guò)push進(jìn)行跳轉(zhuǎn)
      this.$router.push("/menu");
      this.$router.push({ name: "menuLink" });

無(wú)用戶輸入地址時(shí)的默認(rèn)展示組件

  const routes = [{ path: "*", redirect: "/" }];
  或者
  const routes = [{ path: "路徑",name: "名字",redirect: "默認(rèn)展示路徑",components:組件名}];

二級(jí)、三級(jí)路由

  const routes = [{ path: "路徑",name: "名字",component:組件名,children[{path: "路徑",name: "名字",components:組件名}]}];

路由守衛(wèi)

全局守衛(wèi)

router.beforeEach((to, from, next) => {
  //判斷登錄狀態(tài)store.gettes.isLogin === false
  if (to.path == "/login" || to.path == "/register") {
    next();
  } else {
    alert("還沒(méi)有登錄,請(qǐng)先登錄");
    next("/login");
  }
});

后置鉤子(不常用)

router.afterEach((to, from) => {
  alert("after each");
});

路由獨(dú)享守衛(wèi)(設(shè)置在routes中)

beforeEnter: (to, from, next) => {
    alert("非登錄狀態(tài)不能訪問(wèn)此頁(yè)面");
    next(false);
    //判斷登錄狀態(tài)store.gettes.isLogin === false;
    if (to.path == "/login" || to.path == "/register") {
      next();
    } else {
      alert("還沒(méi)有登錄,請(qǐng)先登錄");
      next("/login");
    }
    }

組件內(nèi)守衛(wèi)(設(shè)置在對(duì)應(yīng)組件script中)

//組件內(nèi)守衛(wèi)beforeRouteEnter和beforeRouteLeave方法
  beforeRouteEnter: (to, from, next) => {
    // alert("hello" + this.name);  //這樣name值找不到 因?yàn)槭窍燃虞d組件內(nèi)守衛(wèi)再加載data數(shù)據(jù)的
    // next();
    next(vm => {
      alert("Hello" + vm.name); //想要獲取數(shù)據(jù)需要用這種形式
    });
  }
  beforeRouteLeave: (to, from, next) => {
    if (confirm("確定離開(kāi)嗎") == true) {
      next();
    } else {
      next(false);
    }
  }
//beforeRouteEnter和beforeRouteLeave不能同時(shí)存在

復(fù)用router-view

  const routes = [{ path: "路徑",name: "名字",components:{default:Home,orderringGuide: Orderring,delivery: Dellvery,
  history: Histary}}];

   <router-view name="orderringGuide"></router-view>//default表示默認(rèn)展示  剩余用這種形式展示

路由精簡(jiǎn)

創(chuàng)建routes.js文件
內(nèi)容為
1.引入的路由模塊路徑
2.export const routes = [{ path: "路徑",name: "名字",component:組件名}];
3.在main.js中引入routes.js文件import { routes } from "./routes";

路由控制滾動(dòng)行為

const router = new VueRouter({
  routes,
  mode: "history",
  scrollBehavior(to, from, savedPosition) {
    // return {
    //   x: 0,
    //   y: 100
    // };
    // return {selector:".btn"}
    if (savedPosition) {
      return savedPosition;
    } else {
      return { x: 0, y: 0 };
    }
  }
});
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • VUE Vue :數(shù)據(jù)驅(qū)動(dòng)的M V Vm框架 m :model(后臺(tái)提供數(shù)據(jù)),v :view(頁(yè)面),vM(模板...
    wudongyu閱讀 5,537評(píng)論 0 11
  • Vue八個(gè)生命周期 beforeCreate【創(chuàng)建前】created【創(chuàng)建后】 beforeMount【載入前】 ...
    艾薩克菊花閱讀 1,445評(píng)論 0 12
  • 目錄 - 1.vue-router 動(dòng)態(tài)路由匹配 - 2.router-link組件及其屬性 - 3.vue-ro...
    我跟你蔣閱讀 1,174評(píng)論 0 7
  • 前言 vue-router是什么:是vue.js官方的路由管理器和vue.js的核心深度的集成,讓開(kāi)發(fā)者更加簡(jiǎn)單的...
    GUAN_one閱讀 3,877評(píng)論 0 2
  • 《做到》 如果到結(jié)果隔著做到 是熱愛(ài)和堅(jiān)持的蛻變 不要糾結(jié)于能否做好 那些終究是浮云朵朵 要找到真正方法做到 那才...
    美安然閱讀 241評(píng)論 4 7

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