美文网首页
08-vue-router的使用

08-vue-router的使用

作者: 云桃桃 | 来源:发表于2019-08-01 19:39 被阅读0次

主要从路由设置、重定向、路由嵌套、无相关页面、跳转、入参记录一下。

  • 下载安装vue-router,根目录执行npm install vue-router--save

  • 我的文件目录如下


    path.png
  • main.js全局使用router

import Vue from 'vue'
import App from './App.vue'
import router from './router'     // router实例配置 
import Router from "vue-router";
Vue.use(Router)   // vue全局使用router

Vue.config.productionTip = false

new Vue({
 render: h => h(App),
 router,
}).$mount('#app')
  • router/index.js设置
import Router from 'vue-router'
import routes from './routes'   // router路径设置

export default new Router({
  // 默认是hash, history 可以去掉多余的#号
  mode:'history',
  routes
})
  • routes.js设置
import Home from "@/components/home"
import Loan from "@/components/loan"
import CrerightsChange from "@/components/loan/crerightsChange"
import MoneyInPlan from "@/components/loan/moneyInPlan"
import BulkStanList from "@/components/loan/bulkStanList"
import CrerightsChangeDetails from "@/components/loan/crerightsChangeDetails"


import Account from "@/components/account"
import BorrowAccount from "@/components/account/borrowAccount"
import LoanAccount from "@/components/account/loanAccount"

import CompanyInfo from "@/components/companyInfo"

import Page404 from '@/components/notFound'
import '@/assets/css/global.css'

export default [
    {
        path: '/',
        name: 'home',
        component: Home
    },
    {
        path:'/loan',
        name:'loan',
        // 重定向
        redirect:'/loan/moneyInPlan',
        component:Loan,
        // 嵌套路由用children 来定义
        children:[
            {
                path:'/loan/moneyInPlan',
                name:'moneyInPlan',
                component:MoneyInPlan
            },
            {
                path:'/loan/bulkStanList',
                name:'bulkStanList',
                component:BulkStanList
            },
            {
                path:'/loan/crerightsChange',
                name:'crerightsChange',
                component:CrerightsChange
            },
            { path:'/loan/crerightsChangeDetails',
                name:'crerightsChangeDetails',
                component:CrerightsChangeDetails
            }
        ]
    },
    {
        path:'/account',
        name:'Account',
        redirect:'/account/borrowMoney',
        component:Account,
        children:[
            {
                path:'/account/borrowAccount',
                name:'borrowAccount',
                component:BorrowAccount
            },
            {
                path:'/account/loanAccount',
                name:'loanAccount',
                component:LoanAccount
            }
        ]

    },
    {
        path:'/companyInfo',
        name:'companyInfo',
        component:CompanyInfo
    },
    // 无对应地址时的跳转
    {
        path: '*',
        component: Page404
    }

]
  • 路由跳转和入参
    • router-link标签指定,
      • 用query或者params来指定带的参数,区别在于跳转时候params会以形如?id=XXX的形式出现,query不会出现在地址栏,注意参数里面不能为变量,如果需要变量,请用方法的形式来返回。
       <router-link  :to="{name:'crerightsChangeDetails',query:{id:12}}">立即抢购</router-link>
      
    • js方法指定
      • 字符串形式:表示在routes指定的path: this.$router.push('/')
      • 对象形式this.$router.push({ path: '/' })
      • 命名路由的形式,在routes定义的name:this.$router.push({ name: 'crerightsChangeDetails', params: { id: 666 }})
  • 访问路由参数
    • 在页面访问:路由的name:<p>{{$route.name}}</p>
    • 在vue实例上访问
        methods:{
         // 跳到下一页
          goNext(){
              this.$router.go(1);
          },
          // 跳到上一页
          goPrev(){
            this.$router.go(-1);
          },
          goSomeUrl(){
            this.$router.push('/');
          }
        },
        // 进入路由前
        beforeRouteEnter(to,from,next){
            //当前的页面
            console.log(to);
            // from 从哪个页面进来的
            console.log(from);
            // false 会阻止进入此路由,为空或true为正常进入此路由,
            // 这个不管阻止还是正常进入 必须指定 否则无法正常
            next();
        },
        // 退出路由前
        beforeRouteLeave(to,from,next){
            console.log(to);
            console.log(from);
            next();
        },
        // 更新路由前
        beforeRouteUpdate(to,from,next){
            console.log(to);
            console.log(from);
            next();
        }

相关文章

  • 08-vue-router的使用

    主要从路由设置、重定向、路由嵌套、无相关页面、跳转、入参记录一下。 下载安装vue-router,根目录执行npm...

  • iconfont的使用(下载使用)

    1、下载文件 2、在生命周期中引入项目 beforeCreate () { var domModule = ...

  • Gson的使用--使用注解

    Gson为了简化序列化和反序列化的过程,提供了很多注解,这些注解大致分为三类,我们一一的介绍一下。 自定义字段的名...

  • 记录使用iframe的使用

    默认记录一下----可以说 这是我第一次使用iframe 之前都没有使用过; 使用方式: 自己开发就用了这几个属...

  • with的使用

    下面例子可以具体说明with如何工作: 运行代码,输出如下

  • this的使用

    什么是this? this是一个关键字,这个关键字总是返回一个对象;简单说,就是返回属性或方法“当前”所在的对象。...

  • this的使用

    JS中this调用有几种情况 一:纯粹的函数调用 这是函数的最通常用法,属于全局性调用,因此this就代表全局对象...

  • ==的使用

    积累日常遇到的编码规范,良好的编码习惯,持续更新。。。 日常使用==用于判断的时候,习惯性将比较值写前面,变量写后...

  • this的使用

    1.默认绑定,就是函数立即执行。 函数立即执行就是指向window,但是如果是node环境,就是指向全局conso...

  • %in% 的使用

    写在前面:From 生信技能书向量难点之一:%in% 难点 (1)== 与 %in% 的区别== 强调位置,x和对...

网友评论

      本文标题:08-vue-router的使用

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