资讯专栏INFORMATION COLUMN

Vue.js路由管理器 Vue Router

崔晓明 / 1972人阅读

摘要:可以从其他文件进来定义路由每个路由应该映射一个组件。其中可以是通过创建的组件构造器,或者,只是一个组件配置对象。我们晚点再讨论嵌套路由。通过访问组件实例更新的时候路由离开以通过来取消。路由懒加载参考路由懒加载

起步

HTML




Hello App!

Go to Foo Go to Bar

JavaScript

// 0. 如果使用模块化机制编程,导入Vue和VueRouter,要调用 Vue.use(VueRouter)

// 1. 定义 (路由) 组件。
// 可以从其他文件 import 进来
const Foo = { template: "
foo
" } const Bar = { template: "
bar
" } // 2. 定义路由 // 每个路由应该映射一个组件。 其中"component" 可以是 // 通过 Vue.extend() 创建的组件构造器, // 或者,只是一个组件配置对象。 // 我们晚点再讨论嵌套路由。 const routes = [ { path: "/foo", component: Foo }, { path: "/bar", component: Bar } ] // 3. 创建 router 实例,然后传 `routes` 配置 // 你还可以传别的配置参数, 不过先这么简单着吧。 const router = new VueRouter({ routes // (缩写) 相当于 routes: routes }) // 4. 创建和挂载根实例。 // 记得要通过 router 配置参数注入路由, // 从而让整个应用都有路由功能 const app = new Vue({ router }).$mount("#app") // 现在,应用已经启动了!

通过注入路由器,我们可以在任何组件内通过 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会跳转到/b
const router = new VueRouter({
  routes: [
    { path: "/a", redirect: "/b" }
  ]
})
//重定向的目标也可以是一个命名的路由:
const router = new VueRouter({
  routes: [
    { path: "/a", redirect: { name: "foo" }}
  ]
})
//甚至是一个方法,动态返回重定向目标:
const router = new VueRouter({
  routes: [
    { path: "/a", redirect: to => {
      // 方法接收 目标路由 作为参数
      // return 重定向的 字符串路径/路径对象
    }}
  ]
})
命名路由
export default [
    {
        path:"/",
        redirect:"/app" //默认跳转路由
    },
    {
        path: "/app",
        //路由命名,可用于跳转
        name: "app",
    }
]

//可用于跳转
app
路由元信息

定义路由的时候可以配置 meta 字段:

export default [
    {
        path:"/",
        redirect:"/app" //默认跳转路由
    },
    {
        path: "/app",
        //**相当于HTML的meta标签**
        meta: {
            title: "this is app",
            description: "asdasd"
        },
    }
]
嵌套路由
export default [
    {
        path:"/",
        redirect:"/app" //默认跳转路由
    },
    {
        path: "/app",
        //子路由 匹配 /app/test
         children: [
           {
             path: "test",
             component: Login
           }
         ]
    }
]
路由组件传参
export default [
    {
        path:"/",
        redirect:"/app" //默认跳转路由
    },
    {
        path: "/app/:id", // /app/xxx ,组件内部可以通过$route.params.id拿到这个值
        // 会把:后面的参数通过props传递给组件Todozhong 中
        //布尔模式
        props: true,
        //对象模式
        props:{id:456}
        //函数模式
        props: (route) => ({ id: route.query.b }),
        component: Todo,

    }
]
mode选项(string)

vue-router 默认 hash 模式 —— 使用 URL 的 hash 来模拟一个完整的 URL,于是当 URL 改变时,页面不会重新加载。

如果不想要很丑的 hash,我们可以用路由的 history 模式,这种模式充分利用 history.pushState API 来完成 URL 跳转而无须重新加载页面。

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

这种模式要玩好,还需要后台配置支持。

base(string)

应用的基路径。例如,如果整个单页应用服务在 /app/ 下,然后 base 就应该设为 "/app/"

return new Router({
    routes,
    mode: "history",//默认使用hash#
    base: "/base/", //在path前面都会加上/base/,基路径
  })
linkActiveClass(string)

默认值: "router-link-active"

全局配置 的默认“激活 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"

全局配置 精确激活的默认的 class。

scrollBehavior(Function)

路由跳转后是否滚动

export default () => {
  return new Router({
    routes,
    mode: "history",//默认使用hash#
    base: "/base/", //在path前面都会加上/base/,基路径
    //页面跳转是否需要滚动
    /*
        to:去向路由,完整路由对象
        from:来源路由
        savedPosition:保存的滚动位置
    */
    scrollBehavior (to, from, savedPosition) {
      if (savedPosition) {
        return savedPosition
      } else {
        return { x: 0, y: 0 }
      }
    },
  })
}
parseQuery / stringifyQuery (Function)
/每次import都会创建一个router,避免每次都是同一个router
export default () => {
  return new Router({
    routes,
    mode: "history",//默认使用hash#
    base: "/base/", //在path前面都会加上/base/,基路径
    // 路由后面的参数?a=2&b=3,string->object
     parseQuery (query) {

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

     }
  })
}
fallback(boolean)

当浏览器不支持 history.pushState 控制路由是否应该回退到 hash 模式。默认值为 true。
如果设置为false,则跳转后刷新页面,相当于多页应用

过渡动效

是基本的动态组件,所以我们可以用 组件给它添加一些过渡效果:


  
高级用法 命名视图




const router = new VueRouter({
  routes: [
    {
      path: "/",
      components: {
      //默认组件
        default: Foo,
        //命名组件
        a: Bar,
        b: Baz
      }
    }
  ]
})
导航守卫

全局守卫

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()

// 全局导航守卫(钩子)

// 验证一些用户是否登录
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()
})

// 每次跳转后触发
router.afterEach((to, from) => {
    console.log("after each invoked")
})

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

路由独享的守卫

可以在路由配置上直接定义 beforeEnter 守卫:

export default [
    {
        path:"/",
        redirect:"/app" //默认跳转路由
    },
    {
  
        path: "/app",
        // 路由独享的守卫钩子
        beforeEnter(to, from, next) {
            console.log("app route before enter")
            next()
        }
        component: Todo,
    }
]

组件内的守卫

export default {
  //进来之前
  beforeRouteEnter(to, from, next) {
    // 不!能!获取组件实例 `this`
    // 因为当守卫执行前,组件实例还没被创建
    console.log("todo before enter", this); //todo before enter undefined
    //可以通过传一个回调给 next来访问组件实例。在导航被确认的时候执行回调,并且把组件实例作为回调方法的参数。
    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)
  },
};
路由懒加载

参考:路由懒加载

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/96850.html

相关文章

  • Vue.js实践:一个Node.js+mongoDB+Vue.js的博客内容管理系统

    摘要:项目来源以前曾用过搭建自己的博客网站,但感觉很是臃肿。所以一直想自己写一个博客内容管理器。正好近日看完了各个插件的文档,就用着尝试写了这个简约的博客内容管理器。关于后端后端是用作为服务器的,使用了框架。 项目来源 以前曾用过WordPress搭建自己的博客网站,但感觉WordPress很是臃肿。所以一直想自己写一个博客内容管理器。 正好近日看完了Vue各个插件的文档,就用着Vue尝试写...

    Dr_Noooo 评论0 收藏0
  • Vue.js-状态管理Vuex

    摘要:学习笔记状态管理与状态管理与非父子组件跨级组件和兄弟组件通信时,使用了中央事件总线的一个方法,用来触发和接收事件,进一步起到通信的作用。仓库包含了应用的数据状态和操作过程。新建文件,并写入的配置,会依赖此配置文件来使用编译代码。 学习笔记:状态管理与Vuex 状态管理与Vuex 非父子组件(跨级组件和兄弟组件)通信时,使用了bus(中央事件总线)的一个方法,用来触发和接收事件,进一步...

    lykops 评论0 收藏0
  • vue-router 基础知识点

    摘要:路由模块的本质就是建立起和页面之间的映射关系。模式的原理是事件监测值变化,可以在对象上监听这个事件。这两个方法应用于浏览器记录栈,在当前已有的基础之上,它们提供了对历史记录修改的功能。 vue-router 这里的路由并不是指我们平时所说的硬件路由器,这里的路由就是SPA(单页应用)的路径管理器。再通俗的说,vue-router就是WebApp的链接路径管理系统。vue-router是...

    ningwang 评论0 收藏0
  • vue学习笔记(四)

    摘要:提供了两种向组件传递参数的方式。子路由项路径不要使用开头,以开头的嵌套路径会被当作根路径。路由实例的方法这里学习两个路由实例的方法和。实际上,是通过不同的将这些资源加载后打包,然后输出打包后文件。 一、vue-router 1、简介 我们经常使用vue开发单页面应用程序(SPA)。在开发SPA过程中,路由是必不可少的部分,vue的官方推荐是vue-router。单页面应用程序看起来好像...

    frank_fun 评论0 收藏0
  • vue学习笔记(四)

    摘要:提供了两种向组件传递参数的方式。子路由项路径不要使用开头,以开头的嵌套路径会被当作根路径。路由实例的方法这里学习两个路由实例的方法和。实际上,是通过不同的将这些资源加载后打包,然后输出打包后文件。 一、vue-router 1、简介 我们经常使用vue开发单页面应用程序(SPA)。在开发SPA过程中,路由是必不可少的部分,vue的官方推荐是vue-router。单页面应用程序看起来好像...

    lwx12525 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<