vue路由

作者: very_cute_girls | 来源:发表于2019-12-07 14:32 被阅读0次

#安装

npm install vue-router --save

#创建router.js

import Vue from "vue";
import Router from "vue-router";

//1组件模块
import Lifi from "./components/Lifi.vue";
import Home from "./components/Home.vue";

Vue.use(Router);

export default new Router({
  routes: [
    { path: "/lifi", name: "Lifi", component: Lifi },
    { path: "/home", name: "Home", component: Home }
  ]
});

这里也可以另一种方式

 { path: "/text", name: "text", component:()=>import('../components/text') ,meta: { title: '测试' }}

#在main.js中引入router.js

import router from "./router";

#挂在到vue实例中

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

#给定组件显示的地方

<router-view></router-view>  //组件显示的地方

#使用

<router-link to="/home">主页</router-link>     //切换到指定的组件
<router-link to="/lifi">lifi</router-link>

#也可通过事件的方法来使用路由

methods:{
  toMain() {
    this.$router.push('./main');  //跳转到指定组件
  },
 
  //使用路由返回上一级
  goBack() {
    window.history.length > 1 ? this.$router.go(-1) : this.$router.push("/");
  },
}

#设置默认路由

routes: [
    { path: '*', redirect: '/home' }
  ]

#动态路由方式传值

 { path: '/user/:id', component: User }

#这里记得to前面加冒号 :

<router-link :to="/content/123">{{key}}---{{item}}</router-link>

#获取路由传过来的值(传过来的是一个对象)

 this.$route.params

#get方式传值

 <router-link :to="'/pcontent/?id='+ index">{{index}}---{{item}}</router-link>

#获取传值(一样是对象)

this.$route.query

#编程式导航(几种方式)

this.$router.push({path:'main'});
or
this.$router.push('./main');
or
this.$router.push({name:'main'});
or
this.$router.push({name:'main',params:{id:123}});

#hash模式和history模式

默认是hash模式,如果想改为history模式,实例化router是加上mode
const router = new VueRouter({
  mode: 'history',
  routes: [...]
})

#路由嵌套

几个注意点,嵌套是在上一个路由里嵌套,并且path没有 " / "

routes: [
    { 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
        }
      ]
    }
  ]

相关文章

  • Vue应用

    Vue项目 Vue结构 Vue项目打包与发布 Vue语法二 Vue网络请求 Vue路由 动态路由 编程式路由导航

  • vue路由、自定义指令、脚手架

    *Vue vue-router 一、路由 一、导航式路由 路由路径由

  • Vue路由

    一、Vue路由基础用法: 二、Vue路由配置的抽出 三、路由动态传值: 四、路由的跳转方式: 五、路由的hash模...

  • 手写 Vue Router、手写响应式实现、虚拟 DOM 和 D

    Vue-Router 原理实现 一、Vue-Router 动态路由 二、Vue-Router 嵌套路由 三、Vue...

  • 2018-09-19 vue 八

    一 :路由路由:vue-router是Vue的工具库 vue-router.js下载:npm install ...

  • 6 VUE路由

    vue-> SPA应用,单页面应用(引入vue-router.js) 路由嵌套(多层路由): 路由其他信息:

  • vue路由的介绍(二)--vue动态路由和get的传值

    vue动态路由和get的传值---->同属于路由的传参 1,vue动态路由: 动态路由的配置: ①,在配置路由时加...

  • 2018-09-23 路由

    1.路由 路由:vue-router是Vue的工具库 vue-router.js下载:npm install ...

  • 2018-09-19 Vue 第八天

    1.路由 路由:vue-router是Vue的工具库 vue-router.js下载:npm install ...

  • vue路由

    vue路由--- SPA应用,单页面应用vue-resouce 交互vue-router 路由根据不同url地址...

网友评论

      本文标题:vue路由

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