【问题标题】:Page returns 404 when refreshed, parameter is passed as template页面刷新返回404,参数作为模板传递
【发布时间】:2014-08-24 05:14:57
【问题描述】:

当用户刷新页面或直接访问浏览器中的链接(而不是通过网站中的链接遍历)时,我的角度路径出现问题。

thing.js - 角度工厂

.factory( 'Things', ['$resource', function ( $resource ) {

    return $resource('/api/things/:name', {
        name: '@name'
    }, {
        update: {
            method: 'PUT',
            isArray: true
        },
        get: {
            method: 'GET',
            isArray: true
        }
    });

}])

app.js - 角度路线

.config( function ( $routeProvider, $locationProvider, $httpProvider ) {    
    $locationProvider.html5Mode(true);
    $routeProvider
        .when('/', {
            templateUrl: 'thing/view',
            controller: 'ThingCtrl'
    });

routes.js - 快速路由

app.route( '/api/thing/:name' )
    .get( things.all )
    .put( things.update );
app.route( '/things/*' )
    .get( index.partials );
app.param( 'name', things.load );

index.js - 表达控制器

exports.partials = function( req, res ) {

    var stripped = req.url.split('.')[0];
    var requestedView = path.join('./', stripped);

    res.render( requestedView, function( err, html ) {
        if( err ) {
            console.log( "Error rendering partial '" + requestedView + "'\n", err );
            res.status( 404 );
            res.send( 404 );
        } else {
            res.send( html );
        }
    });
};

错误信息

Error rendering partial 'things/nameOfThing'
{ [Error: Failed to lookup view "things/nameOfThing" in views directory "/Users/johnsmith/projects/theProject/app/views"]
    view:
    { name: 'things/nameOfThing',
      root: '/Users/johnsmith/projects/theProject/app/views',
      defaultEngine: 'html',
      ext: '.html',
      engine: [Function],
      path: undefined } }

我希望刷新使用 things/view.html 模板,但它似乎想使用传递的参数。

【问题讨论】:

    标签: angularjs express routes


    【解决方案1】:

    如果我正确理解您的问题,您必须在 应用配置 中为位置提供程序启用 html5 模式,如下所示

    $locationProvider.html5Mode(true);
    

    将应用程序的基本路径设置为“/”。

    您可以将其更改为 false 以使用基于哈希的位置。

    $locationProvider.html5Mode(false);
    

    【讨论】:

    • 这对我有用,但我确实必须更改所有内部链接(页脚、导航栏)才能使用“/#/”。非常感谢@Prasad!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-30
    • 1970-01-01
    • 1970-01-01
    • 2019-11-27
    • 2013-11-08
    • 1970-01-01
    相关资源
    最近更新 更多