vue路由使用router.push进行路由跳转

更新日期: 2019-09-07阅读: 1.9k标签: 跳转

route-link是在html中静态定义的,也可以在代码中动态跳转:

<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <title>abc</title>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
 
<body>
    <div id="app">
        <h1>Hello App!</h1>
        <!-- 路由出口 -->
        <!-- 路由匹配到的组件将渲染在这里 -->
        <router-view>
        </router-view>
    </div>
</body>
<script type="text/javascript">
// 0. 如果使用模块化机制编程,导入 Vue 和 VueRouter,要调用 Vue.use(VueRouter)
 
// 1. 定义(路由)组件。
// 可以从其他文件 import 进来
// const Foo = { template: '<div @onclick="pushtest" href="">Go to Bar</a>' }
// const Bar = { template: '<div @onclick="pushtest" href="">Go to Foo</a>' }
 
const Foo = Vue.extend({
    template: '<a @click="pushtest" href="javascript:void(0)">Navigate to bar</a>',
    methods: {
        pushtest() {
            //alert("bar");
            this.$router.push({ name: 'bar' });
            //alert("fdas");
        },
    },
 
});
 
const Bar = Vue.extend({
    template: '<a @click="pushtest" href="javascript:void(0)">Navigate to foo</a>',
    methods: {
        pushtest() {
            //alert("foo");
            this.$router.push({ name: 'foo' });
            //alert("fdas");
        },
    },
});
 
// 2. 定义路由
// 每个路由应该映射一个组件。 其中"component" 可以是
// 通过 Vue.extend() 创建的组件构造器,
// 或者,只是一个组件配置对象。
// 我们晚点再讨论嵌套路由。
const routes = [
    { path: '/', redirect: "/bar" },
    { path: '/foo', name: "foo", component: Foo },
    { path: '/bar', name: "bar", component: Bar },
 
]
 
// 3. 创建 router 实例,然后传 `routes` 配置
// 你还可以传别的配置参数, 不过先这么简单着吧。
const router = new VueRouter({
    routes // (缩写)相当于 routes: routes
})
 
// 4. 创建和挂载根实例。
// 记得要通过 router 配置参数注入路由,
// 从而让整个应用都有路由功能
const app = new Vue({
    router, // (缩写)相当于 router: router
    //  methods: {
    //     pushtest:function() {
    //         alert("fdas");
    //     },
    // },
    watch: {
        $route(to, from) {
            //alert(to.path);
            //document.getElementById("testzy").innerText = this.$route.params.id;
        }
    },
 
}).$mount('#app') // 现在,应用已经启动了!
</script>
 
</html>

注意绝对不能写href="",这样执行click跳转后,又会执行href跳转到当前页面
push也可以直接使用path:

this.$router.push('/foo');

push会向history添加一条新记录,此时后退会跳转到前一个组件

router.replace(location)跟push功能类似,但是不会向history添加一条新记录,不能后退

router.go(n)

这个方法的参数是一个整数,意思是在 history 记录中向前或者后退多少步,类似 window.history.go(n)。

// 在浏览器记录中前进一步,等同于 history.forward()
router.go(1)

// 后退一步记录,等同于 history.back()
router.go(-1)

// 前进 3 步记录
router.go(3)

// 如果 history 记录不够用,那就默默地失败呗
router.go(-100)
router.go(100)

链接: https://www.fly63.com/article/detial/5777

浅谈网站无刷新更新技术,基于hash路由的实现。

现在为了提高网站的用户体验,如添加页面切换动画,减少页面加载,很多网站为此都采用了无刷新技术来加载页面内容。

js刷新当前页面,js实现页面自动刷新、跳转的方法总汇

Javascript刷新当前页面:history.go(0)、location.reload()、location=location、location.assign(location)、document.execCommand(Refresh)、location.replace(location)等,Javascript页面自动刷新、跳转,如每隔多少秒刷新一次页面.

js页面跳转另一页的方法——location.href和location.replace用法和区别

window.location.href方式用于跳转到指定页面地址,location.replace将目前浏览器的地址替换掉,调用这个方法的网页,将不会被写入浏览记录。

微信小程序路由跳转

wx.switchTab(Object object)这里的tabBar是底下的导航栏指定的页面,跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面;wx.reLaunch(Object object)基础库 1.1.0 开始支持,低版本需做兼容处理。

angular怎么做前端页面跳转?

Angular中每个页面的显示都需要三个要素:页面的代码,控制器和页面的URL;当要在同一个页面上呈现不同的视图时,这就需要配置路由啦;angular.js已经为我们封装了一个独立的路由工具ng-route;ng-route是靠URL来改变显示的视图的

移动端 location.href 无法成功跳转页面

最近做的移动端页面在请求成功后要跳转页面,通过location.href实现的跳转。但同事在测试时,安卓机可以成功跳转,苹果IOS确无法成功跳转。

javascript如何实现页面跳转?

在JavaScript中有好几种方法可以实现页面跳转,重定向到另一个网页,下面本篇文章就来给大家介绍一些使用JavaScript实现页面跳转的方法,希望对大家有所帮助。

vue利用全局导航守卫作登录后跳转到未登录前指定页面

想进入userManage页面,但是由于没有登录,系统是不会让你进入这个页面,之后会被定向到login页面。但是在登录之后,认为你有这个权限了,就需要重新定向到userManage页面

内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!