【问题标题】:Angular Route for sub folder gives error子文件夹的 Angular Route 给出错误
【发布时间】:2017-04-13 07:28:18
【问题描述】:

我在一个项目中写了 2 个ng-app,一个是user,另一个是admin。 要从 URL 中删除 #,我在两个 app.config 函数中都使用了以下代码

$locationProvider.html5Mode(true);

在 app/index.html 中

<base href="/">

在 app/admin/index.html 中

<base href="/admin/">

用户 app.js

app.config(['$routeProvider','$locationProvider',
    function($routeProvider, $locationProvider) {
        $routeProvider.
        when('/properties', {
            templateUrl: 'views/modules/properties.html'
        })
        .when('/pages', {
            templateUrl: 'views/pages/pages.html'
        })
        .when('pages/editpages', {
            templateUrl: 'views/pages/editPages.html',
            controller: 'editPagesController',
        });

        $locationProvider.html5Mode(true);
    }
]);

server.js

app.use('/', express.static('app', { redirect: false }));

app.get('/*', function(req, res){
    res.sendFile(__dirname + '/app/index.html');
});

如果路径中有额外的参数,我会收到以下错误

Uncaught SyntaxError: Unexpected token

当我的网址为时出现上述错误

http://localhost:8080/properties/
http://localhost:8080/properties/something
http://localhost:8080/pages/
http://localhost:8080/pages/editpages   

如果使用的 url 没有最后一个 / 即,这很好用。

http://localhost:8080/properties
http://localhost:8080/pages

我也参考了这个问题,但无法解决问题

Node / Angular app Uncaught SyntaxError: Unexpected token <

How to use multiple index pages in angular

【问题讨论】:

    标签: javascript angularjs node.js ngroute angularjs-ng-route


    【解决方案1】:

    找到解决方案

    &lt;base href="/"&gt; 的位置正好在 &lt;/head&gt; 之前,在此之前有所有脚本和链接标签。在标题标签后添加&lt;base href="/"&gt; 后问题已解决

    【讨论】:

      【解决方案2】:

      问题出在express中间件上,举个例子

      app.use(express.static('./build/'));
      // Any invalid calls for templateUrls are under app/* and should return 404
      app.use('/app/*', function(req, res, next) {
        four0four.send404(req, res);
      });
      // Any deep link calls should return index.html
      app.use('/*', express.static('./build/index.html'));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-06
        • 1970-01-01
        • 2020-04-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多