vue路由--使用router.push进行路由跳转
手机赚钱怎么赚,给大家推荐一个手机赚钱APP汇总平台:手指乐(http://www.szhile.com/),辛苦搬砖之余用闲余时间动动手指,就可以日赚数百元
- 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路由--使用router.push进行路由跳转的更多相关文章
- vue 使用a+ router.push的形式跳转时,地址栏不显示参数
解决办法: a链接不要写href 属性
- vue中this.$router.push()路由传值和获取的两种常见方法
1.路由传值 this.$router.push() (1) 路由跳转使用router.push()方法,这个方法会向history栈添加一个新纪录,所以,当用户点击浏览器后退按钮时,会回到之前的 ...
- vue 之this.$router.push、replace、go的区别
一.this.$router.push 说明:跳转到指定URL,向history栈添加一个新的记录,点击后退会返回至上一个页面 使用: this.$router.push('/index') this ...
- vue 中 this.$router.push() 路由跳转传参 及 参数接收的方法
传递参数的方法:1.Params 由于动态路由也是传递params的,所以在 this.$router.push() 方法中 path不能和params一起使用,否则params将无效.需要用name ...
- Vue中this.$router.push参数获取(通过路由传参)【路由跳转的方法】
传递参数的方法: 1.Params 由于动态路由也是传递params的,所以在 this.$router.push() 方法中 path不能和params一起使用,否则params将无效.需要用nam ...
- router.push query 路由 跳转 传参使用
this.$router.push({path:'/shop',query:{ goods_name:goods_name, goods_price:goods_price, uid:goods_pr ...
- Vue中this.$router.push(参数) 实现页面跳转
很多情况下,我们在执行点击按钮跳转页面之前还会执行一系列方法,这时可以使用 this.$router.push(location) 来修改 url,完成跳转. push 后面可以是对象,也可以是字符串 ...
- Vue中this.$router.push参数获取
传递参数的方法:1.Params 由于动态路由也是传递params的,所以在 this.$router.push() 方法中path不能和params一起使用,否则params将无效.需要用name来 ...
- vue中this.$router.push() 传参
1 params 传参 注意⚠️:patams传参 ,路径不能使用path 只能使用name,不然获取不到传的数据 this.$router.push({name: 'dispatch', para ...
随机推荐
- Pyhon语法字符集报错
pyhton对语法的字符集,特别的敏感因此我们在写python代码特别要注意.以下代码是个人在学习python中遇见的错误 附:错代码截图1-1 从以上代码中可以看到,似乎代码没有什么问题接下来验证一 ...
- 编写TypeScript工具类型,你需要知道的知识
什么是工具类型 用 JavaScript 编写中大型程序是离不开 lodash 工具的,而用 TypeScript 编程同样离不开工具类型的帮助,工具类型就是类型版的 lodash .简单的来说,就是 ...
- if(a)是什么意思
if(a)等价于 if(a!=0) if(!a)等价于 if(a==0)
- 【Java面试】java基础篇
总结个人的面试经历以及一些网上的的面试题,以供以后面试与巩固java基础. 1.String.StringBuilder和StringBuffer的区别 String用于存储不可变字符串的类,Stri ...
- FastDF step by step
step one 肯定是安装一个FastDF服务了 step two FasDFS配置节点 step third 码代码
- typescript step by step interface class
- JS基础——ATM机终端程序编写(2.0)
在1.0版本上,利用数组进行用户的创建,通过调用数组下标,进行密码.余额的使用.新增了切换账户.修改密码的功能 以下为数组版代码. 创建模拟账户 个账户: let user = ["xiao ...
- linux--->阿里云centos6.9环境配置安装lnmp
阿里云centos6.9环境配置安装lnmp mysql安装 本人博客:http://www.cnblogs.com/frankltf/p/8615418.html PHP安装 1.安装依赖关系 yu ...
- 微信小程序如何创建云函数并安装wx-server-sdk依赖
时间:2020/01/23 步骤 1.在微信开发者工具中云函数所在的文件夹的图标与其他文件夹是不同的,如下(第一个是云函数): 如果需要使一个普通文件变为云函数文件夹,需要在project.confi ...
- 024.Python模块OS模块
一 OS模块 对系统进行操作 1.1 popen 可以把运行的结果,这个字符串转化成utf-8这样的编码格式在进行输出 import os res = os.popen("ifconfig& ...