您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

VueRouter 命名路由

本小节我们介绍如何使用 VueRouter 命名路由。如何定义命名路由、如何使用路由名实现路由。本节的学习相对简单,相信同学们看完本小节,并对小节中的案例自己实现一遍就可以熟练掌握了。

在之前的小节中,我们学习了如何定义路由:

const router = new VueRouter({
  routes: [
    {
      path: '/user',
      component: '[component-name]'
    }
  ]
})

route 对象中有两个。path 表示路由地址,component 表示路由的组件。我们可以在 route 对象中 name 用来给路由指定名字:

const router = new VueRouter({
  routes: [
    {
      path: '/user',
      name: 'user',
      component: '[component-name]'
    }
  ]
})

在之前的小节中,我们学习了使用 <router-link to="path">...</router-link> 的方式来实现路由。实际上 router-link 的 to 可以接收对象:

  <router-link :to="{path: 'path'}">...</router-link>

让我们来看简单的示例:

<!DOCTYPE html>
<html lang="en">
<head>
  < charset="UTF-8">
  < name="viewport" content="width=device-width, initial-scale=1.0">
  < http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <div>
      <router-link :to="{path: '/index'}"></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,欢迎使用网学习 Vue 教程!</div>',
})

const Article = Vue.component('myArticle', {
  template: `<ul><li>1. Vue 计算的学习</li><li>2. React 基础学习</li></ul>`,
})

const routes = [
  { 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>

解释:
HTML 第 12 行,我们定义了,通过对象的形式给 to 赋值。
HTML 第 13 行,我们定义了,通过字符串的形式给 to 赋值。
HTML 第 15 行,我们使用 <router-view></router-view> 组件来渲染匹配组件。
JS 第 5-7 行,我们定义了组件 Index。
JS 第 9-11 行,我们定义了组件 Article。
JS 第 13-16 行,我们定义了路由数组:
- 1. 路由,地址为 ‘/index’,匹配组件 Index。
- 2. 路由,地址为 ‘/article’,匹配组件 Article。

JS 第 18-20 行,创建 router 实例,然后传 routes 配置。
JS 第 24 行,通过 router 配置参数注入路由。

除了通过 path 可以到路由外,还可以通过路由 name 实现:

`<router-link :to="{name: 'name'}">...</router-link>`
<!DOCTYPE html>
<html lang="en">
<head>
  < charset="UTF-8">
  < name="viewport" content="width=device-width, initial-scale=1.0">
  < http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <div>
      <router-link :to="{name: 'index'}"></router-link>
      <router-link :to="{name: '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,欢迎使用网学习 Vue 教程!</div>',
})

const Article = Vue.component('myArticle', {
  template: `<ul><li>1. Vue 计算的学习</li><li>2. React 基础学习</li></ul>`,
})

const routes = [
  { 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>

解释:
HTML 第 12-13 行,我们定义了两个,通过对象的形式给 to 赋值,指定 name 的路由。
HTML 第 15 行,我们使用 <router-view></router-view> 组件来渲染匹配组件。
JS 第 5-7 行,我们定义了组件 Index。
JS 第 9-11 行,我们定义了组件 Article。
JS 第 13-16 行,我们定义了路由数组:

JS 第 18-20 行,创建 router 实例,然后传 routes 配置。
JS 第 24 行,通过 router 配置参数注入路由。

在之前的小节中,我们学习了如何使用 $router 实例来实现编程式的导航。我们也可以使用 $router 实例指定名字的路由地址:

<!DOCTYPE html>
<html lang="en">
<head>
  < charset="UTF-8">
  < name="viewport" content="width=device-width, initial-scale=1.0">
  < http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <div>
      <button @click="jump('index')"></button>
      <button @click="jump('article')"></button>
    </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,欢迎使用网学习 Vue 教程!</div>',
})

const Article = Vue.component('myArticle', {
  template: `<ul><li>1. Vue 计算的学习</li><li>2. Vue 侦听器的学习</li></ul>`,
})


const routes = [
  { 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,
    data() {
    	return {}
    },
    methods: {
      jump(name) {
        this.$router.push({
          name: name
        })
      }
    }
  })
</script>
</html>

解释:
HTML 第 12-13 行,我们定义了两个按钮,并给他们点击事件 jump。
HTML 第 15 行,我们使用 <router-view></router-view> 组件来渲染匹配组件。
JS 第 5-7 行,我们定义了组件 Index。
JS 第 9-11 行,我们定义了组件 Article。
JS 第 13-16 行,我们定义了路由数组:
1. 路由,地址为 ‘/index’,匹配组件 Index。
2. 路由,地址为 ‘/article’,匹配组件 Article。
JS 第 18-20 行,创建 router 实例,然后传 routes 配置。
JS 第 24 行,通过 router 配置参数注入路由。
JS 第 29-31 行,我们定义来 jump ,通过 router.push 实现路由。

本节,我们带大家学习了 VueRouter 命名路由的使用。主要知识点有以下几点:


联系我
置顶