Angular路由ui-router

簡(jiǎn)介

? ? ? ? angularJs自身提供路由ng-router,但是ng-router不是很好用,配置項(xiàng)零散,好比Vue提供的組件傳值一樣,雖然提供給你了用法,但是開(kāi)發(fā)過(guò)程中邏輯一多用著萌萌的,所以我們拋開(kāi)ng-router來(lái)看ui-router。

引入ui-router

? ? ? ? 我們可以去bootCDN搜索ui-router,本地創(chuàng)建js文件,將代碼copy進(jìn)去使用,這樣就可以打入本地使用了,但是要注意的是,Angular的main.js一定要在ui-router之前引用,注意一下先后順序問(wèn)題。

? ? ? ? 例如:

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

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

配置ui-router

? ? //angular.module("moduleName",dep); 定義模塊依賴(兩個(gè)參數(shù))

? ? //angular.module("moduleName"); 獲取模塊 (一個(gè)參數(shù))

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

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

? ? ? ? ? ? //app.config配置項(xiàng)

? ? ? ? ? ? //$stateProvider 狀態(tài)供應(yīng)商,(名字可以看出關(guān)于路由的一系列配置需要由$stateProvider完成)

????????????//$urlRouterProvider 路由重定向

? ??????????$stateProvider.state("home",{

? ? ? ? ? ? ? ? url: "/home"

? ? ? ? ? ? ? ? template: "<h1>首頁(yè)</h1>"

????????????}) .state("about",{

? ??????????????????url: "/about"

? ? ? ? ? ? ? ? ????template: "關(guān)于我們"

????????????});

? ??????????$urlRouterProvider.otherwise("home")

????}])

頁(yè)面配置

? ? <div ui-view></div> ? ?//相當(dāng)于Vue中的插槽,單頁(yè)面應(yīng)用切換路由用來(lái)顯示當(dāng)前路由界面

? ? <a ui-sref="home">首頁(yè)</a> //Angular默認(rèn)會(huì)轉(zhuǎn)換為href

? ? <a ui-sref="about">關(guān)于我們</a>?//Angular默認(rèn)會(huì)轉(zhuǎn)換為href

路由激活狀態(tài)樣式

? ? ? ? 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">首頁(yè)</a>

? ? ? ? ? ? ? ? ? ? <a ui-sref="about" ?ui-sref-active="active">關(guān)于</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: "首頁(yè)"

????????????}) .state("about",{

? ??????????????????url: "/about"

? ? ? ? ? ? ? ? ????template: "關(guān)于我們"

????????????}).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>手機(jī)商品</h1>",

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

? ? ? ? ? ? ? ? ? ? console.log($stateParams);

????????????????????????}]

????????????});

? ??????????$urlRouterProvider.otherwise("home")

? ?}

????????</script>

????</html>

嵌套路由頁(yè)面

? ? ? ? ? ? <div>

? ? ? ? ? ? ? ? ? ? <h1>商品展示</h1>

? ? ? ? ? ? ? ? ? ? <button ng-click="jump()">點(diǎn)擊跳轉(zhuǎn)首頁(yè)</button>

? ? ? ? ? ? ? ? ? ? <a ui-sref="about">跳轉(zhuǎn)至關(guān)于我們</a>

? ? ? ? ? ? ? ? ? ? <button ng-click="jumpOther()">穿參數(shù)</button>

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

? ? ? ? ? ? ? ? ? ? <ul>

? ? ? ? ? ? ? ? ? ? ? ? ? ?//因?yàn)槲覀兺饷娓讣?jí)路由是items所以自路由用items為前綴

? ? ? ? ? ? ? ? ? ? ? ? <li><a ui-sref="items.comp">電腦</a></li>

? ? ? ? ? ? ? ? ? ? ? ? <li><a ui-sref="items.phone">手機(jī)</a></li>

????????????????????</ul>

? ? ? ? ? ? ? ? ? ? <div ui-view></div>

????????????</div> ? ? ? ?

路由花費(fèi)了不少時(shí)間,有不對(duì)的地方希望大家多多提意見(jiàn),博主會(huì)虛心學(xué)習(xí),我相信技術(shù)會(huì)飛速成長(zhǎng),謝謝各位


? ??????????????????????????????????????????????????不積跬步無(wú)以至千里,不積小流無(wú)以成江海?

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容