vue路由管理

安装

介绍

使用模板引擎(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默认没使用路由管理器 安装路由 新建路由配置文件router.js 属性有component、redirec...
    wyc0859阅读 926评论 0 0
  • 基于Vue的一些资料 内容 UI组件 开发框架 实用库 服务端 辅助工具 应用实例 Demo示例 element★...
    尝了又尝阅读 1,190评论 0 1
  • 什么是路由? 网络页面与页面跳转,实现的都是 标签, 标签里面有属性href,给它定义一个网络地址或者路径的...
    廖马儿阅读 2,680评论 1 17
  • 一:什么是闭包?闭包的用处? (1)闭包就是能够读取其他函数内部变量的函数。在本质上,闭包就 是将函数内部和函数外...
    xuguibin阅读 9,781评论 1 52
  • UI组件 element- 饿了么出品的Vue2的web UI工具套件 Vux- 基于Vue和WeUI的组件库 m...
    小姜先森o0O阅读 9,720评论 0 72