美文网首页让前端飞Web前端之路
vue路由--使用router.push进行路由跳转

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

作者: 手指乐 | 来源:发表于2019-09-30 16:54 被阅读0次
  • 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)

相关文章

  • Vue笔记

    1.vue文件之间使用路由跳转传值: this.$router.push({ path:'要跳转的页面路径',...

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

    route-link是在html中静态定义的,也可以在代码中动态跳转: 注意绝对不能写href="",这样执行cl...

  • Vue路由this.$router.push跳转页面不刷新

    一、背景 介绍:在vue项目开发中,使用路由进行页面跳转时,路由所跳转的页面不进行刷新。也就是vue生命周期函数没...

  • Vue

    1.函数路由跳转:this.$router.push('detail') 2. SEO 思路处理 Vue 单页面 ...

  • mpvue页面跳转及传值

    vue中 使用 vue-router 来进行路由跳转的。mpvue中只能通过以下几种方式跳转:(1).a 标签 (...

  • Vue路由重定向

    路由对象$router 路由跳转方法:push() 语法:$router.push('url') 分写与合写方式的...

  • vue-router

    跳转路由 vue路由文件router.js 在vue文件中使用路由: 第一种方式我们可以看到name是给路由命名,...

  • nuxt在js中使用路由

    使用nuxt的时候在js进行路由跳转的方法 使用 this.$router.push在我的项目中行不通,原因thi...

  • Vue方向:Vue路由跳转时组件的属性

    1、通过标签进行路由跳转 1.1 标签跳转路由的方式 Vue Router提供了两个内置的组件帮助我们进行路由的跳...

  • vue路由vue-router快速入手

    路由是vue进行“页面跳转”的实现方式,而vue-router是vue的一个路由组件。https://router...

网友评论

    本文标题:vue路由--使用router.push进行路由跳转

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