程序员

Angular路由ui-router

2018-07-16  本文已影响0人  全村的希望iOS

简介

        angularJs自身提供路由ng-router,但是ng-router不是很好用,配置项零散,好比Vue提供的组件传值一样,虽然提供给你了用法,但是开发过程中逻辑一多用着萌萌的,所以我们抛开ng-router来看ui-router。

引入ui-router

        我们可以去bootCDN搜索ui-router,本地创建js文件,将代码copy进去使用,这样就可以打入本地使用了,但是要注意的是,Angular的main.js一定要在ui-router之前引用,注意一下先后顺序问题。

        例如:

         <script src="angular.main.js"></script> 

        <script src="angular-ui-router.js"></script>

配置ui-router

    //angular.module("moduleName",dep); 定义模块依赖(两个参数)

    //angular.module("moduleName"); 获取模块 (一个参数)

    var app = angular.module("myApp",["ui-router"]);

    app.config(["$stateProvider","$urlRouterProvider",function($stateProvider){

            //app.config配置项

            //$stateProvider 状态供应商,(名字可以看出关于路由的一系列配置需要由$stateProvider完成)

            //$urlRouterProvider 路由重定向

            $stateProvider.state("home",{

                url: "/home"

                template: "<h1>首页</h1>"

            }) .state("about",{

                    url: "/about"

                    template: "关于我们"

            });

            $urlRouterProvider.otherwise("home")

    }])

页面配置

    <div ui-view></div>    //相当于Vue中的插槽,单页面应用切换路由用来显示当前路由界面

    <a ui-sref="home">首页</a> //Angular默认会转换为href

    <a ui-sref="about">关于我们</a> //Angular默认会转换为href

路由激活状态样式

        ui-sref-active="active"

完整代码

    <html ng-app="myApp">

        <head>

                <style>

                    .active{

                                color: red

                    }

                </style>

                <script src="angular.main.js"></script>

                <script src="angular-ui-router.js"></script>

        </head>

        <body>

            <div ui-view></div>

            <footer>

                    <a ui-sref="home"  ui-sref-active="active">首页</a>

                    <a ui-sref="about"  ui-sref-active="active">关于</a>

                    <a ui-sref="items">商品</a>

            </footer>

        </body>

        <script>

                var app = angular.module("myApp", [ui-router]);                        app.config(["$stateProvider","$urlRouterProvider",function($stateProvider){

        $stateProvider.state("home",{

                url: "/home"

                template: "首页"

            }) .state("about",{

                    url: "/about"

                    template: "关于我们"

            }).state("items",{//牛逼的潜逃路由

                    url: "/items",

                    templateUrl: "./items.html",

                    controller:["$scope",$state,function($scope,$state){

                            $scope.jump = function(){

                                    $state.go("home");

                            }

                            $scope.jumpOther = function() {

                                $state.go("items.phone",{

                                        id: "phone"

                                    });

                            }

                    }]

            }).state("items.comp",{

                    url: "/comp",

                    template: "<h1>电脑商品</h1>"

            }).state("item.phone",{

                        url:"phone/:id",

                        template:"<h1>手机商品</h1>",

                        controller:["$scope","$stateParams",function($scope,$stateParams){

                    console.log($stateParams);

                        }]

            });

            $urlRouterProvider.otherwise("home")

   }

        </script>

    </html>

嵌套路由页面

            <div>

                    <h1>商品展示</h1>

                    <button ng-click="jump()">点击跳转首页</button>

                    <a ui-sref="about">跳转至关于我们</a>

                    <button ng-click="jumpOther()">穿参数</button>

                    <a ui-sref="items.other({id:"sref"})"></a>

                    <ul>

                           //因为我们外面父级路由是items所以自路由用items为前缀

                        <li><a ui-sref="items.comp">电脑</a></li>

                        <li><a ui-sref="items.phone">手机</a></li>

                    </ul>

                    <div ui-view></div>

            </div>        

路由花费了不少时间,有不对的地方希望大家多多提意见,博主会虚心学习,我相信技术会飞速成长,谢谢各位


                                                    不积跬步无以至千里,不积小流无以成江海 

上一篇下一篇

猜你喜欢

热点阅读