千峰商城-springboot项目搭建-47-router编程式导航

追求适度,才能走向成功;人在顶峰,迈步就是下坡;身在低谷,抬足既是登高;弦,绷得太紧会断;人,思虑过度会疯;水至清无鱼,人至真无友,山至高无树;适度,不是中庸,而是一种明智的生活态度。

导读:本篇文章讲解 千峰商城-springboot项目搭建-47-router编程式导航,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

js代码实现路由跳转:编程式导航。

1.push() (字符串做参数)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript" src="js/vue.js" ></script>
        <script type="text/javascript" src="js/vue-router.js"></script>
    </head>
    <body>
        <div id="container">
            <button type="button" @click="test">按钮</button>
            <router-view></router-view>
        </div>
        
        <script type="text/javascript">
            
            const t1 = {
                template:`<div style="width:400px; height:200px; border:blue 1px solid">index</div>`
            };
            
            const myrouter = new VueRouter({
                routes:[{path:"/a",component:t1,}
            ]});

            var vm = new Vue({
                el:"#container",
                router:myrouter,
                methods:{
                    test:function(){
                        //js代码实现路由跳转:编程式导航
                        myrouter.push("/a");
                    }
                }
            });
        </script>
    </body>
</html>

2.push() (对象做参数)

 

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript" src="js/vue.js" ></script>
        <script type="text/javascript" src="js/vue-router.js"></script>
    </head>
    <body>
        <div id="container">
            <button type="button" @click="test">按钮</button>
            <router-view></router-view>
        </div>
        
        <script type="text/javascript">
            
            const t1 = {
                template:`<div style="width:400px; height:200px; border:blue 1px solid">index</div>`
            };
            
            const myrouter = new VueRouter({
                routes:[{path:"/a",component:t1,}
            ]});

            var vm = new Vue({
                el:"#container",
                router:myrouter,
                methods:{
                    test:function(){
                        //js代码实现路由跳转:编程式导航
                        //1.字符串做参数
                        //myrouter.push("/a");
                        //2.对象做参数
                        myrouter.push({path:"/a"});
                    }
                }
            });
        </script>
    </body>
</html>

 3.
有参数的路由跳转(name做参数,要指定路由的name)
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript" src="js/vue.js" ></script>
        <script type="text/javascript" src="js/vue-router.js"></script>
    </head>
    <body>
        <div id="container">
            <button type="button" @click="test">按钮</button>
            <router-view></router-view>
        </div>
        
        <script type="text/javascript">
            
            const t1 = {
                template:`<div style="width:400px; height:200px; border:blue 1px solid">index:{{$route.params.id}}</div>`
            };
            
            const myrouter = new VueRouter({
                routes:[{path:"/a",name:"r1",component:t1,}
            ]});

            var vm = new Vue({
                el:"#container",
                router:myrouter,
                methods:{
                    test:function(){
                        //js代码实现路由跳转:编程式导航
                        //1.字符串做参数
                        //myrouter.push("/a");
                        //2.对象做参数
                        //myrouter.push({path:"/a"});
                        
                        //3.有参数的路由跳转
                        myrouter.push({name:"r1",params:{id:101}});
                    }
                }
            });
        </script>
    </body>
</html>

 千峰商城-springboot项目搭建-47-router编程式导航

4.url传值:/a?id=101

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript" src="js/vue.js" ></script>
        <script type="text/javascript" src="js/vue-router.js"></script>
    </head>
    <body>
        <div id="container">
            <button type="button" @click="test">按钮</button>
            <router-view></router-view>
        </div>
        
        <script type="text/javascript">
            
            const t1 = {
                template:`<div style="width:400px; height:200px; border:blue 1px solid">index:{{$route.params.id}}</div>`
            };
            
            const myrouter = new VueRouter({
                routes:[{path:"/a",component:t1,}
            ]});

            var vm = new Vue({
                el:"#container",
                router:myrouter,
                methods:{
                    test:function(){
                        //js代码实现路由跳转:编程式导航
                        
                        //1.字符串做参数
                        //myrouter.push("/a");
                        
                        //2.对象做参数
                        //myrouter.push({path:"/a"});
                        
                        //3.有参数的路由跳转
                        //myrouter.push({name:"r1",params:{id:101}});
                        
                        //4.url传值:/a?id=101
                        myrouter.push({path:"/a",query:{id:101}});
                    }
                }
            });
        </script>
    </body>
</html>

 千峰商城-springboot项目搭建-47-router编程式导航

5.不会产生浏览器历史记录的replace()。(功能与push()一致)

myrouter.push("/a");//会产生历史记录

 千峰商城-springboot项目搭建-47-router编程式导航

myrouter.replace("/a");//不会产生浏览器历史记录

千峰商城-springboot项目搭建-47-router编程式导航

 6.go():参数传递为一个整数,表示在浏览器历史记录中前进或后退多少步。相当于window.history.go(-1)的作用。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript" src="js/vue.js" ></script>
        <script type="text/javascript" src="js/vue-router.js"></script>
    </head>
    <body>
        <div id="container">
            <button type="button" @click="test">按钮1</button>
            <button type="button" @click="test2">back</button>
            <router-view></router-view>
        </div>
        
        <script type="text/javascript">
            
            const t1 = {
                template:`<div style="width:400px; height:200px; border:blue 1px solid">index:{{$route.params.id}}</div>`
            };
            
            const myrouter = new VueRouter({
                routes:[{path:"/a",component:t1,}
            ]});

            var vm = new Vue({
                el:"#container",
                router:myrouter,
                methods:{
                    test:function(){
                        //js代码实现路由跳转:编程式导航
                        
                        //1.字符串做参数
                        myrouter.push("/a");//会产生历史记录
                        //myrouter.replace("/a");//不会产生浏览器历史记录
                        
                        //2.对象做参数
                        //myrouter.push({path:"/a"});
                        
                        //3.有参数的路由跳转
                        //myrouter.push({name:"r1",params:{id:101}});
                        
                        //4.url传值:/a?id=101
                        //myrouter.push({path:"/a",query:{id:101}});
                    },
                    test2:function(){
                        //在浏览器的历史记录中前进或后退
-1);
                    }
                    
                    
                }
            });
        </script>
    </body>
</html>

 千峰商城-springboot项目搭建-47-router编程式导航千峰商城-springboot项目搭建-47-router编程式导航千峰商城-springboot项目搭建-47-router编程式导航

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/128128.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!