Vue Router相关属性

**1,**映射配置
在index.js里创建的VueRouter对象中写入相关的hash路径和相关Vue组件

const routes = [{
        path: '',
        //重定向设置,默认显示的hash为home
        redirect: '/home'
    },
    {
        path: '/home',
        component: home
    },
    {
        path: '/about',
        component: about
    }
]

然后再app.vue中添加router-link和router-view

 <!-- tag可以设置组价的样式。默认为a标签 repalce可以禁用页面返回和前进 -->
    <router-link to='/home' tag="button" replace="">index</router-link>
    <router-link to='/about' tag="button" replace="">about</router-link>
    <router-view></router-view>

**2,**将默认的hash修改为history的方法
使用hash属性时在打开网页后会出现#分隔
在这里插入图片描述
改为history属性后可以解决此问题
在index.js中创建的router对象里加入mode属性并将其值设为history

const router = new VueRouter({
    //配置路由和组件之间的对应关系
    routes,
    //可以将默认的hash值改为history值
    mode: 'history'
})

在这里插入图片描述
**3,**通过代码实现路由跳转
不用router-link实现路由跳转仅通过代码实现路由跳转的方法如下所示

<template>
  <div id="app">
    <button @click="homeClick">index</button>
    <button @click="aboutClick">about</button>
    <router-view></router-view>
  </div>
</template>

<script>

export default {
  name: 'App',
  methods: {
    //用代码的方式实现路由跳转
    homeClick(){
      this.$router.push('/home')
    },
    aboutClick(){
      this.$router.push('/about')
    }
  },
}

注:以上文件名均是在安装脚手架后默认的文件名

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值