第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

VueRouter路由別名、重定向

1. 前言

本小節(jié)我們介紹如何使用 VueRouter 路由別名、重定向。路由別名和重定向在項(xiàng)目中經(jīng)常使用,本節(jié)的學(xué)習(xí)內(nèi)容相對(duì)簡(jiǎn)單,相信同學(xué)們看完本小節(jié),并對(duì)小節(jié)中的案例自己實(shí)現(xiàn)一遍就可以熟練掌握了。

2. 路由重定向

重定向也是通過(guò) routes 配置來(lái)完成,可以配置路由重定向到具體路由地址、具名路由或者動(dòng)態(tài)返回重定向目標(biāo)。

2.1 重定向到路由地址

通過(guò)屬性 redirect 指定重定向的路由地址:

const router = new VueRouter({
  routes: [
    { path: '/a', redirect: '/b' }
  ]
})

示例:

實(shí)例演示
預(yù)覽 復(fù)制
復(fù)制成功!
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <div>
      <router-link to="/index">首頁(yè)</router-link>
      <router-link to="/article">文章</router-link>
    </div>
    <router-view></router-view>
  </div>
</body>

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

const Index = Vue.component('index', {
  template: '<div>Hello,歡迎使用慕課網(wǎng)學(xué)習(xí) Vue 教程!</div>',
})

const Article = Vue.component('myArticle', {
  template: `<ul><li>1. Vue 計(jì)算屬性的學(xué)習(xí)</li><li>2. React 基礎(chǔ)學(xué)習(xí)</li></ul>`,
})

const routes = [
  { path: '/', redirect: '/index' },
  { path: '/index', component: Index },
  { path: '/article', component: Article }
]

const router = new VueRouter({
  routes: routes
})

var vm = new Vue({
  el: '#app',
  router: router,
  data() {
    return {}
  }
})
</script>
</html>

運(yùn)行案例 點(diǎn)擊 "運(yùn)行案例" 可查看在線(xiàn)運(yùn)行效果

代碼解釋?zhuān)?/strong>
HTML 代碼第 12-13 行,我們定義了兩個(gè)跳轉(zhuǎn)鏈接。
HTML 代碼第 15 行,我們使用 <router-view></router-view> 組件來(lái)渲染匹配組件。
JS 代碼第 5-7 行,我們定義了組件 Index。
JS 代碼第 9-11 行,我們定義了組件 Article。
JS 代碼第 13-17 行,我們定義了路由數(shù)組:

  1. 根路由,地址為 ‘/’,重定向到路由地址 ‘/index’。
  2. 首頁(yè)路由,地址為 ‘/index’,匹配組件 Index。
  3. 文章路由,地址為 ‘/article’,匹配組件 Article。

JS 代碼第 19-21 行,創(chuàng)建 router 實(shí)例,然后傳 routes 配置。
JS 代碼第 25 行,通過(guò) router 配置參數(shù)注入路由。

2.2 重定向到具名路由

通過(guò)屬性 redirect 重定向到具名路由:

const router = new VueRouter({
  routes: [
    { path: '/a', redirect: {name: 'name'} }
  ]
})

示例:

實(shí)例演示
預(yù)覽 復(fù)制
復(fù)制成功!
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <div>
      <router-link to="/index">首頁(yè)</router-link>
      <router-link to="/article">文章</router-link>
    </div>
    <router-view></router-view>
  </div>
</body>

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

const Index = Vue.component('index', {
  template: '<div>Hello,歡迎使用慕課網(wǎng)學(xué)習(xí) Vue 教程!</div>',
})

const Article = Vue.component('myArticle', {
  template: `<ul><li>1. Vue 計(jì)算屬性的學(xué)習(xí)</li><li>2. React 基礎(chǔ)學(xué)習(xí)</li></ul>`,
})

const routes = [
  { path: '/', redirect: {name: 'index'} },
  { path: '/index', name: 'index', component: Index },
  { path: '/article', name: 'article', component: Article }
]

const router = new VueRouter({
  routes: routes
})

var vm = new Vue({
  el: '#app',
  router: router,
  data() {
    return {}
  }
})
</script>
</html>

運(yùn)行案例 點(diǎn)擊 "運(yùn)行案例" 可查看在線(xiàn)運(yùn)行效果

代碼解釋?zhuān)?/strong>
HTML 代碼第 12-13 行,我們定義了兩個(gè)跳轉(zhuǎn)鏈接。
HTML 代碼第 15 行,我們使用 <router-view></router-view> 組件來(lái)渲染匹配組件。
JS 代碼第 5-7 行,我們定義了組件 Index。
JS 代碼第 9-11 行,我們定義了組件 Article。
JS 代碼第 13-17 行,我們定義了路由數(shù)組:

  1. 根路由,地址為 ‘/’,重定向到具名路由 ‘index’。
  2. 首頁(yè)路由,地址為 ‘/index’,匹配組件 Index。
  3. 文章路由,地址為 ‘/article’,匹配組件 Article。

JS 代碼第 19-21 行,創(chuàng)建 router 實(shí)例,然后傳 routes 配置。
JS 代碼第 25 行,通過(guò) router 配置參數(shù)注入路由。

2.3 動(dòng)態(tài)返回重定向目標(biāo)

屬性 redirect 可以接收一個(gè)方法,動(dòng)態(tài)返回重定向目標(biāo):

const router = new VueRouter({
  routes: [
    { path: '/a', redirect: to => {
      // 方法接收 目標(biāo)路由 作為參數(shù)
      // return 重定向的 字符串路徑/路徑對(duì)象
    }}
  ]
})

示例:

實(shí)例演示
預(yù)覽 復(fù)制
復(fù)制成功!
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <div>
      <router-link to="/index">首頁(yè)</router-link>
      <router-link to="/article">文章</router-link>
    </div>
    <router-view></router-view>
  </div>
</body>

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

const Index = Vue.component('index', {
  template: '<div>Hello,歡迎使用慕課網(wǎng)學(xué)習(xí) Vue 教程!</div>',
})

const Article = Vue.component('myArticle', {
  template: `<ul><li>1. Vue 計(jì)算屬性的學(xué)習(xí)</li><li>2. React 基礎(chǔ)學(xué)習(xí)</li></ul>`,
})

const routes = [
{ path: '/', redirect: to => {
  if(Math.random() > 0.5) {
    return '/index'
  }else {
    return {
      name: 'article'
    }
  }
}},
  { path: '/index', name: 'index', component: Index },
  { path: '/article', name: 'article', component: Article }
]

const router = new VueRouter({
  routes: routes
})

var vm = new Vue({
  el: '#app',
  router: router,
  data() {
    return {}
  }
})
</script>
</html>

運(yùn)行案例 點(diǎn)擊 "運(yùn)行案例" 可查看在線(xiàn)運(yùn)行效果

代碼解釋?zhuān)?/strong>
HTML 代碼第 12-13 行,我們定義了兩個(gè)跳轉(zhuǎn)鏈接。
HTML 代碼第 15 行,我們使用 <router-view></router-view> 組件來(lái)渲染匹配組件。
JS 代碼第 5-7 行,我們定義了組件 Index。
JS 代碼第 9-11 行,我們定義了組件 Article。
JS 代碼第 13-25 行,我們定義了路由數(shù)組:

  1. 根路由,地址為 ‘/’,根據(jù)隨機(jī)數(shù)的大小重定向到路由 ‘/index’ 或 ‘/article’。
  2. 首頁(yè)路由,地址為 ‘/index’,匹配組件 Index。
  3. 文章路由,地址為 ‘/article’,匹配組件 Article。

JS 代碼第 27-29 行,創(chuàng)建 router 實(shí)例,然后傳 routes 配置。
JS 代碼第 32 行,通過(guò) router 配置參數(shù)注入路由。

3. 路由別名

“重定向”的意思是,當(dāng)用戶(hù)訪(fǎng)問(wèn) /a 時(shí),URL 將會(huì)被替換成 /b,然后匹配路由為 /b,那么“別名”又是什么呢?
/a 的別名是 /b,意味著,當(dāng)用戶(hù)訪(fǎng)問(wèn) /b 時(shí),URL 會(huì)保持為 /b,但是路由匹配則為 /a,就像用戶(hù)訪(fǎng)問(wèn) /a 一樣。

const router = new VueRouter({
  routes: [
    { path: '/a', component: A, alias: '/b' }
  ]
})

示例:

實(shí)例演示
預(yù)覽 復(fù)制
復(fù)制成功!
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <div>
      <router-link to="/index">首頁(yè)</router-link>
      <router-link to="/article">文章</router-link>
    </div>
    <router-view></router-view>
  </div>
</body>

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

const Index = Vue.component('index', {
  template: '<div>Hello,歡迎使用慕課網(wǎng)學(xué)習(xí) Vue 教程!</div>',
})

const Article = Vue.component('myArticle', {
  template: `<ul><li>1. Vue 計(jì)算屬性的學(xué)習(xí)</li><li>2. React 基礎(chǔ)學(xué)習(xí)</li></ul>`,
})

const routes = [
  { path: '/index', component: Index, alias: '/' },
  { path: '/article', component: Article }
]

const router = new VueRouter({
  routes: routes
})

var vm = new Vue({
  el: '#app',
  router: router,
  data() {
    return {}
  }
})
</script>
</html>

運(yùn)行案例 點(diǎn)擊 "運(yùn)行案例" 可查看在線(xiàn)運(yùn)行效果

代碼解釋?zhuān)?/strong>
HTML 代碼第 12-13 行,我們定義了兩個(gè)跳轉(zhuǎn)鏈接。
HTML 代碼第 15 行,我們使用 <router-view></router-view> 組件來(lái)渲染匹配組件。
JS 代碼第 5-7 行,我們定義了組件 Index。
JS 代碼第 9-11 行,我們定義了組件 Article。
JS 代碼第 13-16 行,我們定義了路由數(shù)組:

  1. 首頁(yè)路由,地址為 ‘/index’,匹配組件 Index,路由別名 ‘/’。
  2. 文章路由,地址為 ‘/article’,匹配組件 Article。

JS 代碼第 18-20 行,創(chuàng)建 router 實(shí)例,然后傳 routes 配置。
JS 代碼第 24 行,通過(guò) router 配置參數(shù)注入路由。

4. 小結(jié)

本節(jié),我們帶大家學(xué)習(xí)了 VueRouter 路由重定向和別名。主要知識(shí)點(diǎn)有以下幾點(diǎn):

  • 通過(guò) redirect 屬性指定路由重定向地址。
  • 通過(guò) alias 屬性配置路由別名。