前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >17.编程式导航(js驱动跳转)和路由的hash模式与History模式

17.编程式导航(js驱动跳转)和路由的hash模式与History模式

作者头像
玩蛇的胖纸
发布2019-10-21 18:11:34
6180
发布2019-10-21 18:11:34
举报

编程式导航

与router-link导航相比,router-link类似于a标签,编程式导航相当于ajax,导航用页面跳转

Home.vue

代码语言:javascript
复制
<template>
    <div>
        <h2>{{msg}}</h2>
        <br>
        <button @click="goNew()">通过js跳转到新闻页面</button>
        <br>
    </div>
</template>
<script>

export default {
  name: 'home',  
  data () {
    return {
        msg:'首页组件'
    }
  },
  methods:{
      goNew(){
        //   this.$router.push({path:'news'})
          this.$router.push({path:'vcontent/0'})
      }

  },
  components:{
  }
}
</script>
<style lang="scss" scoped>
h2{
    color: red;
}
</style>

hash模式与History模式

在main.js中加入:

代码语言:javascript
复制
mode:'history',//hash改为history模式

 main.js

代码语言:javascript
复制
import Vue from 'vue';
import App from './App.vue';

import VueResource from 'vue-resource';
Vue.use(VueResource)


import VueRouter from 'vue-router';
Vue.use(VueRouter)

// 1.创建组件,导入组件
import Home from './components/Home.vue';
import News from './components/News.vue';
import vContent from './components/vContent.vue';

import Good from './components/Goods.vue';

// 2.配置路由
const routes=[
    {path:'/home',component:Home},
    {path:'/news',component:News},

      {path:'/vcontent/:aid',component:vContent}, //动态路由

      {path:'*',redirect:'/home'}, //默认路由跳转到首页

      {path:'/goods',component:Good},
]
//注意,这里是routes,而不是routers

// 3.实例化VueRouter
const router=new VueRouter({
      mode:'history',//hash改为history模式
    routes//(缩写)相当于routers:routers
})

// 4.挂载
new Vue({
  el: '#app',
  router,
  render: h => h(App)
})

// 5.将<router-view></router-view>放在App.vue里面
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-10-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 编程式导航
    • 与router-link导航相比,router-link类似于a标签,编程式导航相当于ajax,导航用页面跳转
      • Home.vue
      • hash模式与History模式
        • 在main.js中加入:
          •  main.js
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档