简体   繁体   English

Angular html5mode 无法按预期工作

[英]Angular html5mode does not work as expected

In my angular app all the URLs are like this:在我的 angular 应用中,所有的 URL 都是这样的:

testsite.com/#/standard-page

And I would like them to be like:我希望他们像:

testsite.com/standard-page

I have read about adding the html5mode to my config, I now have this:我已经阅读了关于将 html5mode 添加到我的配置中的内容,我现在有了这个:

angular.module('myApp').config(['$stateProvider', '$urlRouterProvider', '$locationProvider', config]);

function config($stateProvider, $urlRouterProvider, $locationProvider) {
    $locationProvider.html5Mode(true);

    $stateProvider
        .state('index', {
            url: '/',
            templateUrl: siteInfo.templateRoot + '/startPage.html',
        })
        .state('standard-page', {
            resolve: {
                standardPageData: getStandardPageData
            },
            url: '/standard-page',
            templateUrl: siteInfo.templateRoot + '/standardPage.html',
            controller: 'StandardPageController',
            controllerAs: 'StandardPageCtrl'
        });
};

and I added <base href="/"> to the head of my head, but it does not work completely as expected yet.我将<base href="/">到我的头上,但它还没有完全按预期工作。

When I go to testsite.com/#/standard-page in my browser it loads the page, then it modifies the URL to testsite.com/standard-page in the address bar.当我在浏览器中访问testsite.com/#/standard-page ,它会加载页面,然后将 URL 修改为地址栏中的testsite.com/standard-page However, if I type in directly ``testsite.com/standard-page`, it gives me a 404.但是,如果我直接输入“testsite.com/standard-page”,它会给我一个 404。

So I'm almost there, just not quite yet.所以我快到了,只是还没到。 Any ideas what I might have forgotten to do here?有什么想法我可能忘记在这里做什么了吗?

Angular can't do all the job. Angular 无法完成所有工作。 When you write testsite.com/standard-page , your server must handle the request and respond correctly.当您编写testsite.com/standard-page ,您的服务器必须处理请求并正确响应。 Your server respond 404 so your browser can't display anything.您的服务器响应 404,因此您的浏览器无法显示任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM