javascript - 初学angularJS+express,路由路径中/转换成%2F,导致路径失效,求原因?

查看:72
本文介绍了javascript - 初学angularJS+express,路由路径中/转换成%2F,导致路径失效,求原因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

页面打开如下

点击电脑链接,显示url路径如下

点击后

期望的效果如下:

我的代码如下 app.js,用的是"express": "^4.15.2"

var express = require('express');
var path = require('path');

var app = express();
//使用静态文件服务器中间件
app.use(express.static(path.join(__dirname,'app/public')));
app.listen(8080);

test.html


    <html ng-app="routingDemoApp">
<head>
    <meta charset="utf-8">
    <title>AngularJS 路由实例 - 菜鸟教程</title>
    <script src="/lib/jquery/dist/jquery.js"></script>
    <script src="/lib/bootstrap/dist/js/bootstrap.js"></script>
    <script src="/lib/angular/angular.js"></script>
    <script src="/lib/angular-route/angular-route.js"></script>
    <script>
        angular.module('routingDemoApp',['ngRoute'])
            .config(['$locationProvider','$routeProvider', function config($locationProvider, $routeProvider){

                $routeProvider
                    .when('/',{
                        template:'这是首页页面'
                    }).
                        when('/computers',{
                            template:'这是电脑分类页面'
                }).
                        when('/printers',{
                            template:'这是打印机页面'
                }).
                        otherwise('/');
            }]);
    </script>
</head>
<body>

<h2>AngularJS 路由应用</h2>
<ul>
    <li><a href="#/">首页</a></li>
    <li><a href="#/computers">电脑</a></li>
    <li><a href="#/printers">打印机</a></li>
    <li><a href="#/blabla">其他</a></li>
    <li><a href="#/printers">w3school</a></li>
</ul>

<div ng-view></div>



</body>
</html>

被这个问题困了很久,求大神指个方向,谢谢

解决方案

这个是angular1.6默认给hash路由上添加了!(感叹号),导致出错,修改方法如下(添加配置,去掉默认前缀感叹号):

    angular.module('routingDemoApp',['ngRoute'])
    .config(['$routeProvider', function($routeProvider){
        $routeProvider
        .when('/',{template:'这是首页页面'})
        .when('/computers',{template:'这是电脑分类页面'})
        .when('/printers',{template:'这是打印机页面'})
        .otherwise({redirectTo:'/'});
    }])
    //添加如下配置
    .config(['$locationProvider', function($locationProvider) {
        $locationProvider.hashPrefix("");
    }]);

这篇关于javascript - 初学angularJS+express,路由路径中/转换成%2F,导致路径失效,求原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆