欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

淺談Vue.js路由管理器VueRouter

起步

創(chuàng)新互聯(lián)公司基于分布式IDC數(shù)據(jù)中心構(gòu)建的平臺為眾多戶提供聯(lián)通機房服務(wù)器托管 四川大帶寬租用 成都機柜租用 成都服務(wù)器租用。

HTML

<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>

<div id="app">
 <h2>Hello App!</h2>
 <p>
  <!-- 使用 router-link 組件來導(dǎo)航. -->
  <!-- 通過傳入 `to` 屬性指定鏈接. -->
  <!-- <router-link> 默認會被渲染成一個 `<a>` 標簽 -->
  <router-link to="/foo">Go to Foo</router-link>
  <router-link to="/bar">Go to Bar</router-link>
 </p>
 <!-- 路由出口 -->
 <!-- 路由匹配到的組件將渲染在這里 n內(nèi)置組件-->
 <router-view></router-view>
</div>

JavaScript

// 0. 如果使用模塊化機制編程,導(dǎo)入Vue和VueRouter,要調(diào)用 Vue.use(VueRouter)

// 1. 定義 (路由) 組件。
// 可以從其他文件 import 進來
const Foo = { template: '<div>foo</div>' }
const Bar = { template: '<div>bar</div>' }

// 2. 定義路由
// 每個路由應(yīng)該映射一個組件。 其中"component" 可以是
// 通過 Vue.extend() 創(chuàng)建的組件構(gòu)造器,
// 或者,只是一個組件配置對象。
// 我們晚點再討論嵌套路由。
const routes = [
 { path: '/foo', component: Foo },
 { path: '/bar', component: Bar }
]

// 3. 創(chuàng)建 router 實例,然后傳 `routes` 配置
// 你還可以傳別的配置參數(shù), 不過先這么簡單著吧。
const router = new VueRouter({
 routes // (縮寫) 相當于 routes: routes
})

// 4. 創(chuàng)建和掛載根實例。
// 記得要通過 router 配置參數(shù)注入路由,
// 從而讓整個應(yīng)用都有路由功能
const app = new Vue({
 router
}).$mount('#app')

// 現(xiàn)在,應(yīng)用已經(jīng)啟動了!

通過注入路由器,我們可以在任何組件內(nèi)通過 this.$router 訪問路由器,也可以通過 this.$route 訪問當前路由:

export default {
 computed: {
  username () {
   // 我們很快就會看到 `params` 是什么
   return this.$route.params.username
  }
 },
 methods: {
  goBack () {
   window.history.length > 1
    ? this.$router.go(-1)
    : this.$router.push('/')
  }
 }
}

routes 選項 (Array)

redirect(重定向 )

//此時訪問/a會跳轉(zhuǎn)到/b
const router = new VueRouter({
 routes: [
  { path: '/a', redirect: '/b' }
 ]
})
//重定向的目標也可以是一個命名的路由:
const router = new VueRouter({
 routes: [
  { path: '/a', redirect: { name: 'foo' }}
 ]
})
//甚至是一個方法,動態(tài)返回重定向目標:
const router = new VueRouter({
 routes: [
  { path: '/a', redirect: to => {
   // 方法接收 目標路由 作為參數(shù)
   // return 重定向的 字符串路徑/路徑對象
  }}
 ]
})

命名路由

export default [
  {
    path:'/',
    redirect:'/app' //默認跳轉(zhuǎn)路由
  },
  {
    path: '/app',
    //路由命名,可用于跳轉(zhuǎn)
    name: 'app',
  }
]

//可用于跳轉(zhuǎn)
<router-link :to="{name:'app'}">app</router-link>

路由元信息

定義路由的時候可以配置 meta 字段:

export default [
  {
    path:'/',
    redirect:'/app' //默認跳轉(zhuǎn)路由
  },
  {
    path: '/app',
    //**相當于HTML的meta標簽**
    meta: {
      title: 'this is app',
      description: 'asdasd'
    },
  }
]

嵌套路由

export default [
  {
    path:'/',
    redirect:'/app' //默認跳轉(zhuǎn)路由
  },
  {
    path: '/app',
    //子路由 匹配 /app/test
     children: [
      {
       path: 'test',
       component: Login
      }
     ]
  }
]

路由組件傳參

export default [
  {
    path:'/',
    redirect:'/app' //默認跳轉(zhuǎn)路由
  },
  {
    path: '/app/:id', // /app/xxx ,組件內(nèi)部可以通過$route.params.id拿到這個值
    // 會把:后面的參數(shù)通過props傳遞給組件Todozhong 中
    //布爾模式
    props: true,
    //對象模式
    props:{id:456}
    //函數(shù)模式
    props: (route) => ({ id: route.query.b }),
    component: Todo,

  }
]

mode選項(string)

vue-router 默認 hash 模式 —— 使用 URL 的 hash 來模擬一個完整的 URL,于是當 URL 改變時,頁面不會重新加載。

如果不想要很丑的 hash,我們可以用路由的 history 模式,這種模式充分利用 history.pushState API 來完成 URL 跳轉(zhuǎn)而無須重新加載頁面。

const router = new VueRouter({
 mode: 'history',
 routes: [...]
})

這種模式要玩好,還需要后臺配置支持。

base(string)

應(yīng)用的基路徑。例如,如果整個單頁應(yīng)用服務(wù)在 /app/ 下,然后 base 就應(yīng)該設(shè)為 "/app/"

return new Router({
  routes,
  mode: 'history',//默認使用hash#
  base: '/base/', //在path前面都會加上/base/,基路徑
 })

linkActiveClass(string)

默認值: "router-link-active"

全局配置 <router-link> 的默認“激活 class 類名”。

return new Router({
  routes,
  mode: 'history',//默認使用hash#
  base: '/base/', //在path前面都會加上/base/,基路徑
  // 點擊calss名字
  linkActiveClass: 'active-link', //匹配到其中一個子集
  linkExactActiveClass: 'exact-active-link',//完全匹配
 })

linkExactActiveClass(string)

默認值: "router-link-exact-active"

全局配置 <router-link> 精確激活的默認的 class。

scrollBehavior(Function)

路由跳轉(zhuǎn)后是否滾動

export default () => {
 return new Router({
  routes,
  mode: 'history',//默認使用hash#
  base: '/base/', //在path前面都會加上/base/,基路徑
  //頁面跳轉(zhuǎn)是否需要滾動
  /*
    to:去向路由,完整路由對象
    from:來源路由
    savedPosition:保存的滾動位置
  */
  scrollBehavior (to, from, savedPosition) {
   if (savedPosition) {
    return savedPosition
   } else {
    return { x: 0, y: 0 }
   }
  },
 })
}

parseQuery / stringifyQuery (Function)

/每次import都會創(chuàng)建一個router,避免每次都是同一個router
export default () => {
 return new Router({
  routes,
  mode: 'history',//默認使用hash#
  base: '/base/', //在path前面都會加上/base/,基路徑
  // 路由后面的參數(shù)?a=2&b=3,string->object
   parseQuery (query) {

   },
   //object->string
  stringifyQuery (obj) {

   }
 })
}

fallback(boolean)

當瀏覽器不支持 history.pushState 控制路由是否應(yīng)該回退到 hash 模式。默認值為 true。
如果設(shè)置為false,則跳轉(zhuǎn)后刷新頁面,相當于多頁應(yīng)用

<router-link>

過渡動效

<router-view> 是基本的動態(tài)組件,所以我們可以用 <transition> 組件給它添加一些過渡效果:

<transition>
 <router-view></router-view>
</transition>

高級用法

命名視圖

<router-view class="view one"></router-view>
<router-view class="view two" name="a"></router-view>
<router-view class="view three" name="b"></router-view>

const router = new VueRouter({
 routes: [
  {
   path: '/',
   components: {
   //默認組件
    default: Foo,
    //命名組件
    a: Bar,
    b: Baz
   }
  }
 ]
})

導(dǎo)航守衛(wèi)

全局守衛(wèi)

import Vue from 'vue'
import VueRouter from 'vue-router'

import App from './app.vue'

import './assets/styles/global.styl'
// const root = document.createElement('div')
// document.body.appendChild(root)
import createRouter from './config/router'
Vue.use(VueRouter)

const router = createRouter()

// 全局導(dǎo)航守衛(wèi)(鉤子)

// 驗證一些用戶是否登錄
router.beforeEach((to, from, next) => {
  console.log('before each invoked')
  next()
//  if (to.fullPath === '/app') {
//   next({ path: '/login' })
//   console.log('to.fullPath :'+to.fullPath )

//  } else {
//   next()
//  }
})

router.beforeResolve((to, from, next) => {
  console.log('before resolve invoked')
  next()
})

// 每次跳轉(zhuǎn)后觸發(fā)
router.afterEach((to, from) => {
  console.log('after each invoked')
})

new Vue({
  router,
  render: (h) => h(App)
}).$mount("#root")

路由獨享的守衛(wèi)

可以在路由配置上直接定義 beforeEnter 守衛(wèi):

export default [
  {
    path:'/',
    redirect:'/app' //默認跳轉(zhuǎn)路由
  },
  {
 
    path: '/app',
    // 路由獨享的守衛(wèi)鉤子
    beforeEnter(to, from, next) {
      console.log('app route before enter')
      next()
    }
    component: Todo,
  }
]

組件內(nèi)的守衛(wèi)

export default {
 //進來之前
 beforeRouteEnter(to, from, next) {
  // 不!能!獲取組件實例 `this`
  // 因為當守衛(wèi)執(zhí)行前,組件實例還沒被創(chuàng)建
  console.log("todo before enter", this); //todo before enter undefined
  //可以通過傳一個回調(diào)給 next來訪問組件實例。在導(dǎo)航被確認的時候執(zhí)行回調(diào),并且把組件實例作為回調(diào)方法的參數(shù)。
  next(vm => {
    // 通過 `vm` 訪問組件實例
   console.log("after enter vm.id is ", vm.id);
  });
 },
 //更新的時候
 beforeRouteUpdate(to, from, next) {
  console.log("todo update enter");
  next();
 },
 // 路由離開
 beforeRouteLeave(to, from, next) {
  console.log("todo leave enter");
  const answer = window.confirm('Do you really want to leave? you have unsaved changes!')
   if (answer) {
    next()
   } else {
    //以通過 next(false) 來取消。
    next(false)
   }
 },
 props:['id'],
 components: {
  Item,
  Tabs
 },
 mounted() {
  console.log(this.id)
 },
};

路由懶加載

參考:路由懶加載

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

本文名稱:淺談Vue.js路由管理器VueRouter
文章位置:http://chinadenli.net/article46/jsighg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、微信公眾號、軟件開發(fā)、企業(yè)網(wǎng)站制作、電子商務(wù)、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)
国产欧美亚洲精品自拍| 欧美国产日韩变态另类在线看| 日本加勒比不卡二三四区| 九九热精彩视频在线免费| 国产三级欧美三级日韩三级| 人体偷拍一区二区三区| 亚洲一区二区三区四区| 中日韩免费一区二区三区| 熟女体下毛荫荫黑森林自拍| 国产免费一区二区不卡| 色丁香一区二区黑人巨大| 日本免费熟女一区二区三区| 免费亚洲黄色在线观看| 少妇视频一区二区三区| 国产高清一区二区不卡| 亚洲少妇一区二区三区懂色| 91久久精品在这里色伊人| 午夜精品福利视频观看| 欧美六区视频在线观看| 白白操白白在线免费观看| 91日韩欧美在线视频| 东京不热免费观看日本| 日韩欧美第一页在线观看| 国产精品激情在线观看| 日本加勒比在线播放一区| 欧美av人人妻av人人爽蜜桃| 亚洲av日韩av高潮无打码| 丝袜破了有美女肉体免费观看| 日韩在线视频精品视频| 国产午夜福利不卡片在线观看| 黄色片一区二区在线观看| 欧美一区日韩二区亚洲三区| 亚洲欧美日本视频一区二区| 精品欧美日韩一区二区三区 | 九九热九九热九九热九九热| 爽到高潮嗷嗷叫之在现观看| 亚洲中文字幕高清视频在线观看| 熟妇久久人妻中文字幕| 少妇成人精品一区二区| 精品丝袜一区二区三区性色| 亚洲五月婷婷中文字幕|