欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

vue-router的使用方法及含参数的配置方法

程序员文章站 2023-11-26 15:41:52
html router-link:跳转链接 参数to:就是跳转到的链接位置 二层链接 eg:

html

router-link:跳转链接

参数to:就是跳转到的链接位置

二层链接 eg:

<router-link to="/users/evan">/users/evan</router-link>

需要配置所对应的对应的children

children所对应的参数

path:可分我固定的参数url 和带参数的  区别于 :(冒号)

name:对应的参数的模块名称(动态传参数)

component:可以传多个组件

eg:
{ path: '/',
 // a single route can define multiple named components
 // which will be rendered into <router-view>s with corresponding names.
 components: {
  default: foo,
  a: bar,
  b: baz
 }
},

:to="{ name: 'user', params: { username: 'evan' }, query: { foo: 'bar' }}"

参数说明params 对应的是children的path的值 ,to中的name为vuerouter中的name

当要传多个参数时

{ path: ':username/:aaa', name: 'user', component: user }

也可以利用query传值

query: { foo: 'bar' }
eg: path: ':username'

redirect:链接直接指向指定的方向(重定向)

beforeenter:
·
{ path: '/dashboard', component: dashboard, beforeenter: requireauth },
function requireauth (route, redirect, next) {
 if (!auth.loggedin()) {
 redirect({
  path: '/login',
  query: { redirect: route.fullpath }
 })
 } else {
 next()
 }
}

使用props将组件和路由解耦:

在组件中使用$route会使之与其对应路由形成高度耦合,从而使组件只能在某些特定的url上使用,限制了其灵活性。

对应的参数的值的获取

{{$route.params.username1 }}
 { path: '/users', component: users,
  children: [
  { path: ':username', name: 'user', component: user }
  ]
 }

eg:

 <ul>
  <li><router-link to="/">/</router-link></li>
  <li><router-link to="/" exact>/ (exact match)</router-link></li>

  <li><router-link to="/users">/users</router-link></li>
  <li><router-link to="/users" exact>/users (exact match)</router-link></li>

  <li><router-link to="/users/evan">/users/evan</router-link></li>
  <li><router-link to="/users/evan#foo">/users/evan#foo</router-link></li>
  <li>
   <router-link :to="{ path: '/users/evan', query: { foo: 'bar' }}">
   /users/evan?foo=bar
   </router-link>
  </li>
  <li><!-- #635 -->
   <router-link :to="{ name: 'user', params: { username: 'evan' }, query: { foo: 'bar' }}" exact>
   /users/evan?foo=bar (named view + exact match)
   </router-link>
  </li>
  <li>
   <router-link :to="{ path: '/users/evan', query: { foo: 'bar', baz: 'qux' }}">
   /users/evan?foo=bar&baz=qux
   </router-link>
  </li>

  <li><router-link to="/about">/about</router-link></li>

  <router-link tag="li" to="/about">
   <a>/about (active class on outer element)</a>
  </router-link>
  </ul>
const router = new vuerouter({
 mode: 'history',
 base: __dirname,
 routes: [
 { path: '/', component: home },
 { path: '/about', component: about },
 { path: '/users', component: users,
  children: [
  { path: ':username', name: 'user', component: user }
  ]
 }
 ]
})

js:vue-router配置

router-view:组件的放置位置

对应的链接所对应的组件的配置

路由底下的子组件

{ path: '/users', component: users,
  children: [
  { path: ':username', name: 'user', component: user }
  ]
 }

//组件

vue.use(vuerouter)

const home = { template: '<div><h2>home</h2></div>' }
const about = { template: '<div><h2>about</h2></div>' }

const users = {
 template: `
 <div>
  <h2>users</h2>
  <router-view></router-view>
 </div>
 `
}

const user = { template: '<div>{{ $route.params.username }}</div>' }

==================分界线====================

案例:1

<!doctype html>
<html lang="en">
<head>
 <meta charset="utf-8">
 <title>title</title>
</head>
<body>
<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">
 <h1>hello app!</h1>
 <p>
  <!-- 使用 router-link 组件来导航. -->
  <!-- 通过传入 `to` 属性指定链接. -->
  <!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->
  <router-link to="/foo">go to foo</router-link>
  <router-link to="/bar">go to bar</router-link>
 </p>
 <!-- 路由出口 -->
 <!-- 路由匹配到的组件将渲染在这里 -->
 <router-view></router-view>
</div>
<script>
 var foo = { template: '<div>foo</div>' }
 var home= { template:'<div>home</div>' }
 var bar = { template: '<div>bar</div>' }

 // 2. 定义路由
 // 每个路由应该映射一个组件。 其中"component" 可以是
 // 通过 vue.extend() 创建的组件构造器,
 // 或者,只是一个组件配置对象。
 // 我们晚点再讨论嵌套路由。
 var routes = [
  { path: '/', component: home},
  { path: '/foo', component: foo },
  { path: '/bar', component: bar }
 ]

 // 3. 创建 router 实例,然后传 `routes` 配置
 // 你还可以传别的配置参数, 不过先这么简单着吧。
 var router = new vuerouter({
  routes // (缩写)相当于 routes: routes
 })

 // 4. 创建和挂载根实例。
 // 记得要通过 router 配置参数注入路由,
 // 从而让整个应用都有路由功能
 var app = new vue({
  router,
  el:'#app'
 })
</script>
</body>
</html>

实现效果

vue-router的使用方法及含参数的配置方法

=====================================

案例:2

path的二级链接固定参数

<div id="app">
 <h1>data fetching</h1>
 <ul>
 <li><router-link to="/">/</router-link></li>
 <li><router-link to="/user/even" exact>/users (exact match)</router-link></li>
</ul>
 <router-view class="view"></router-view>
</div>
<script>
 var users = {
  template: '<div><h2>users</h2><router-view></router-view> </div>'
 }
 var user = {
  template: '<div>{{$route.params.username1 }}</div>'
 }
 var home = {
  template: '<div>home</div>'
 }
 var router = new vuerouter({
  routes: [
   { path: '/', component: home,},
   // 动态路径参数 以冒号开头
   { path: '/user', component: users,
    children: [
     { path: ':username1', name: 'bbb', component: user }
    ]}
  ]
 })
 new vue({
  el:"#app",
  router,
  data:{
   name:111
  }
 })

vue-router的使用方法及含参数的配置方法

二级链接动态参数

<div id="app">
 <h1>data fetching</h1>
 <ul>
 <li><router-link to="/">/</router-link></li>
 <li><router-link :to="{ name: 'bbb', params: { username: name}}" exact>/users/evan?foo=bar (named view + exact match)
  </router-link>
 </li>
</ul>
 <router-view class="view"></router-view>
</div>
<script>
 var users = {
  template: '<div><h2>users</h2><router-view></router-view> </div>'
 }
 var user = {
  template: '<div>{{$route.params.username }}</div>'
 }
 var home = {
  template: '<div>home</div>'
 }
 var router = new vuerouter({
  routes: [
   { path: '/', component: home,},
   // 动态路径参数 以冒号开头
   { path: '/user', component: users,
    children: [
     { path: ':username', name: 'bbb', component: user }
    ]}
  ]
 })
 new vue({
  el:"#app",
  router,
  data:{
   name:111
  }
 })
</script>

实现效果

vue-router的使用方法及含参数的配置方法 

注意与上一个实例进行对比发现链接的地址并没有发生改变,刷新后会返回首页

如何解决这个问题呢

==============案例=======================

<div id="app">
 <h1>data fetching</h1>
 <ul>
 <li><router-link to="/">/</router-link></li>
 <li><router-link :to='{name:"user", params:{ id:111 }}'>111</router-link>
 </li>
</ul>
 <router-view class="view"></router-view>
</div>
<script>
 var users = {
  template: '<div><h2>users</h2><div>{{$route.params.id}}</div></div>'
 }
 var home = {
  template: '<div>home</div>'
 }
 var router = new vuerouter({
  routes: [
   { path: '/', component: home,},
   // 动态路径参数 以冒号开头
   { path: '/user/:id', name:'user', component: users}
  ]
 })
 new vue({
  el:"#app",
  router,
  data:{
   name:111
  }
 })
</script>

vue-router的使用方法及含参数的配置方法

====================案例传多个组件==============================

<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">
 <h1>named views</h1>
 <ul>
  <li><router-link to="/">/</router-link></li>
  <li><router-link to="/other">/other</router-link></li>
 </ul>
 <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>
</div>
<script>

 var foo = { template: '<div>foo</div>' }
 var bar = { template: '<div>bar</div>' }
 var baz = { template: '<div>baz</div>' }

 var router = new vuerouter({
  mode: 'history',
  routes: [
   { path: '/',
    // a single route can define multiple named components
    // which will be rendered into <router-view>s with corresponding names.
    components: {
     default: foo,
     a: bar,
     b: baz
    }
   },
   {
    path: '/other',
    components: {
     default: baz,
     a: bar,
     b: foo
    }
   }
  ]
 })
 // 4. 创建和挂载根实例。
 // 记得要通过 router 配置参数注入路由,
 // 从而让整个应用都有路由功能
 var app = new vue({
  router,
  el:'#app'
 })
</script>

vue-router的使用方法及含参数的配置方法

总结

以上所述是小编给大家介绍的vue-router的使用方法及含参数的配置方法,希望对大家有所帮助