美文网首页
vue路由管理

vue路由管理

作者: 一点金光 | 来源:发表于2019-08-22 13:01 被阅读0次

安装

介绍

使用模板引擎(vuejs),我们实现了通过组合组件来组成应用程序。

使用路由引擎(vue-router),实现把组件与路由联系起来。

使用

// 1.定义组件
const Foo = { template: '<div>foo</div>' }
const Bar = { template: '<div>bar</div>' }

// 2.定义路由
const routes = [
  { path: '/foo', component: Foo },
  { path: '/bar', component: Bar }
]

// 3.创建路由器(组件和路由握手)
const router = new VueRouter({
  routes // 缩写 routes: routes
})

// 4.挂载根实例(路由引擎和模板引擎握手)
const app = new Vue({
  router
}).$mount('#app')

参考


//定义路由-静态路由(一个路由对应一个组件)
{ path: '/foo', component: Foo }

//定义路由-动态路由(多个路由对应一个组件)
//(路径参数)
{ path: '/user/:id', component: User }

//定义路由-匿名路由
{ path: '/user/:id', component: User}
//定义路由-命名路由
{ path: '/user/:id', component: User,name: 'user' }

//定义路由-别名路由(多个路由对应一个组件)
 { path: '/a', component: A, alias: '/b' }
//定义路由-定向路由(多个路由对应一个组件)
//(重新定向)
{ path: '/a', redirect: '/b' }//字符
{ path: '/a', redirect: { name: 'foo' }}//对象
{ path: '/a', redirect: to=>{return '/b'}}//函数
{ path: '/a', redirect: to=>{return { name: 'foo' }}}
// 用户访问 /a时,URL将会被替换成 /b,然后匹配路由为 /b

//定义路由-嵌套路由(一个路由,多个视图,视图嵌套)
 { path: '/user/:id', component: User,
      children: [
        {
          // 当 /user/:id/profile 匹配成功,
          // UserProfile 会被渲染在 User 的 <router-view> 中
          path: 'profile',
          component: UserProfile
        },
        {
          // 当 /user/:id/posts 匹配成功
          // UserPosts 会被渲染在 User 的 <router-view> 中
          path: 'posts',
          component: UserPosts
        }
      ]
    }
//定义路由-命名视图(一个路由,多个视图,视图同级)  
{
    path: '/',
    components: {
        //默认
        default: Foo,
        //视图名字a
        a: Bar,
        b: Baz
    }
}
//定义路由-嵌套视图
{
  path: '/settings',
  component: UserSettings,
  children: [
  //"/settings/emails"=>UserEmailsSubscriptions
  {
    path: 'emails',
    component: UserEmailsSubscriptions
  }, 
  //"/settings/profile"=>UserProfile
  {
    path: 'profile',
    components: {
      default: UserProfile,
      helper: UserProfilePreview
    }
  }]
} 

//定义路由-数据路由(传递数据)
{
      path: '/user/:id',
      components: { default: User, sidebar: Sidebar },
      props: { default: true, sidebar: false }
}

// 定义链接-声明式
<router-link :to="{name: 'user',params: {userId:123 }}">User</router-link>
// 定义链接-编程式
//方式2 router.push(location, onComplete?, onAbort?)
router.push({name:'user', params:{ userId: 123 }})

// 定义钩子-全局-前置
router.beforeEach((to,from, next)=> {
    // ...
    //进入下个钩子:next()
    //中断当前钩子:next(false)
    //跳转其他路由:next('/');//next({ path: '/' })
})
// 定义钩子-全局-后置
router.afterEach((to,from) =>{
    //...
})

// 定义钩子-路由-前置
{
    path: '/foo',
    component: Foo,
    beforeEnter: (to, from,next)=> {
    // ...
    }
}
// 定义钩子-组件-前置
{
    template: '<div>foo</div>'
    beforeRouteEnter: (to, from,next)=> {
    // ...
    }
}

// 访问路由-组件内
this.$route

// 定义视图-命名视图
<router-view></router-view>
<router-view name="a"></router-view>
<router-view name="b"></router-view>

访问路由器-组件内
this.$router

// 定义路由-获取数据
// 导航完成之前获取:导航完成前,在路由进入的守卫中获取数据,在数据获取成功后执行导
航。

// 定义组件-获取数据
// 导航完成之后获取:先完成导航,然后在接下来的组件生命周期钩子中获取数据。在数据获取
期间显示『加载中』之类的指示。

相关文章

  • Vue 学习笔记 vue-router路由和前端状态管理

    Vue 学习笔记十一、vue-router路由和前端状态管理 什么是路由:网址 11.1 vue­-router路...

  • 前端理论面试-router+axios

    Vue Router Vue Router 是 Vue.js 官方的路由管理器。 包含的功能有: 嵌套的路由/视图...

  • Vue-router

    Vue路由详解 一、Vue Router 是 Vue.js 官方的路由管理器。它和 Vue.js 的核心深度集成,...

  • Vue 相关链接汇总

    官网 Vue 官网 Vuex 官网 (状态管理) Vue Router (路由管理) Vue Cli 相关 axi...

  • Vue之vue-router路由

    八、vue-router路由 目录:使用、测试 1.使用 1)说明Vue Router是Vue.js官方的路由管理...

  • vue基础

    vue全家桶 vue-cli 框架 vue vuex 状态管理 vue-router 路由 axios 请求...

  • day03:Vue Router

    写在前面 Vue Router 是 Vue.js 官方的路由管理器,我们用它来完成前端的路由管理。 PART01:...

  • 关于vue--路由(公众号开发总结)

    关于路由 在router.js中引入vue和vue-router 路由模式 前端路由就是一个前端不同页面的状态管理...

  • vue-router

    什么是 路由是单页面应用(SPA)的路径管理器。 vue-router是vue的官方路由插件,和vue.js是深度...

  • 简单实现vue-router

    vue-router 将路由引入到前端让构建单页应用变得简单,vue-router作为Vue的路由管理器,使我们在...

网友评论

      本文标题:vue路由管理

      本文链接:https://www.haomeiwen.com/subject/qxlneftx.html