GVKun编程网logo

页面刷新时使用Angular JS ui.router html5Mode(true)配置Amazon S3静态站点(html 亚马逊)

23

如果您想了解页面刷新时使用AngularJSui.routerhtml5Mode和true配置AmazonS3静态站点的知识,那么本篇文章将是您的不二之选。我们将深入剖析页面刷新时使用AngularJ

如果您想了解页面刷新时使用Angular JS ui.router html5Modetrue配置Amazon S3静态站点的知识,那么本篇文章将是您的不二之选。我们将深入剖析页面刷新时使用Angular JS ui.router html5Mode的各个方面,并为您解答true配置Amazon S3静态站点的疑在这篇文章中,我们将为您介绍页面刷新时使用Angular JS ui.router html5Mode的相关知识,同时也会详细的解释true配置Amazon S3静态站点的运用方法,并给出实际的案例分析,希望能帮助到您!

本文目录一览:

页面刷新时使用Angular JS ui.router html5Mode(true)配置Amazon S3静态站点(html 亚马逊)

页面刷新时使用Angular JS ui.router html5Mode(true)配置Amazon S3静态站点(html 亚马逊)

如何配置Amazon S3静态网页以正确路由Angular ui.router
html5Mode路由?在页面刷新时,它将请求一个不存在的文件,而angular无法处理该文件。在文档中,他们建议更改服务器上的URL重写。

https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-
to-configure-your-server-to-work-with-html5mode

但是,S3是存储设备,并且不提供与尝试使用内置重定向规则相同的重定向选项,例如

<RoutingRules>    <RoutingRule>        <Condition>            <HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals >        </Condition>        <Redirect>             <HostName>[[ your application''s domain name ]]</HostName>             <ReplaceKeyPrefixWith>#/</ReplaceKeyPrefixWith>        </Redirect>    </RoutingRule></RoutingRules>

但是,这只会导致重定向循环。

有什么建议?

答案1

小编典典

基本上有3个选项,使用EC2实例对配置的HTML5路由执行实际的服务器重写,或者像dnozay建议的那样,使用后备模式并重写请求以使用#!。哈希邦。最后,您可以只使用标准角度路线,这是我选择的选项。麻烦较少,当Angular2.0推出时,您可以进行更新。

这里并没有真正解决路由问题。

Angular1位置html5mode等效于angular2

Angular1位置html5mode等效于angular2

我试图找到我可以在angular2中启用 html5mode的地方.不幸的是,无法从文档或任何地方找到它.我正在使用gulp和browser-sync来加载我的文件.
我见过很多人说使用connect-modrewrite之类的东西

browserSync.instance = browserSync.init({
    server: {
      baseDir: ['app'],middleware: [
        proxyMiddleware,modRewrite([
          '^[^\\.]*$/index.html [L]'
        ])
      ]
    },});

我设置它,但它仍然无法工作,我得到404除了主要网址之外的任何东西.
这有什么线索吗?

解决方法

您正在寻找的是 PathLocationStrategy,这是默认的LocationStrategy

注意

使用此功能时,您需要将服务器设置为将所有请求重定向到根URL.例如,如果您的用户请求myApp.com/someRoute,则将从该网址解析资产,这是不正确的.相反,您希望服务器提供index.html页面而不管指定的路由,但仍保持路由完整,以便客户端应用程序可以使用它.

另外一定要设置你的< base path =“/”/>标记在HTML的头部.

Angularjs $location html5mode浅析

Angularjs $location html5mode浅析

关于HTML5 history API

参考资料:http://diveintohtml5.info/history.html
参考资料为英文,内容较为简单,周末会做简单翻译,方便英语吃力的童鞋。

H5之前,一个URL对应一个页面,无论以何种方式修改地址栏地址,都会导致页面完全刷新,无论跳转页面之间是否有关联。简单来说,H5的history API提供接口,可以使用javascript修改地址栏路径,而不刷新页面。

使用场景

简化说明:未设置静态缓存。

假设后端数据输出模式为:获取数据---读取模板---渲染模板。不同页面之间共享模板,只是数据内容不同。在没有H5的新接口之前,不同页面之间的跳转,则相同的脚本,CSS文件会重新加载,不仅加剧服务器压力,在慢速网络下,用户体验也会变差。在有H5接口之后,不同路径跳转,可以拦截超链接默认行为,通过脚本控制地址栏,通过Ajax加载数据,在前端完成数据呈现,就能避免不必要的资源加载时间。

Demo浅析

  • 数据源
    /static/articles/index.json;
    /static/articles/node.json;
    /static/articles/javascript.json
json// index.json
{
  "title": "Frontend develop language",
  "content": "FE encounter lots of trouble"
}
// node.json
{
  "title": "Node theme develop",
  "content": "whether believe or not, node changes frontend develop way"
}
// javascript.json
{
  "title": "Javascript theme develop",
  "content": "maybe javascript just tools, it enhance programmer skill"
}
  • 模板
    /views/article.jade
    (临时看的jade,所以简单为上)
jadedoctype html
html
  head
     title Shuffle History
     script(src="/libs/superagent.js")
     script(src="/js/initialize.js") 
  body
     h3#title
       | #{title}
     p#content
       | #{content}
     p
       a(href="/") Index entrance
     p
       a(href="/node/" id="node") Node entrance
     p
       a(href="/javascript/" id="javascript") Javascript entrance

总计三个页面,共享同一个jade模板。数据源对应关系为
/ ---------- index.json
/node/ --- node.json
/javascript --- javascript.json

若直接访问/,/node/, javascript页面,express读取对应数据源,渲染模板,然后直接输出html,代码如下:

javascript/**
 * Created by jasonborn on 14/11/19.
 */
var jade = require(''jade'');
var path = require(''path'');
var express = require(''express'');
var app = express();
app.set(''env'', ''development'');
app.engine(''jade'', jade.__express);

app.get(''/'', function(req, res) {
  res.render(''article.jade'', require(''./static/articles/index.json''), function(err, html) {
    res.type(''html'');
    res.send(html);
  })
});

app.get(''/node/'', function(req, res) {
  res.render(''article.jade'', require(''./static/articles/node.json''), function(err, html) {
    res.type(''html'');
    res.send(html);
  })
});

app.get(''/javascript/'', function(req, res) {
  res.render(''article.jade'', require(''./static/articles/javascript.json''), function(err, html) {
    res.type(''html'');
    res.send(html);
  })
});

app.use(express.static(path.join(__dirname, ''static'')));

app.listen(1336);

自此实现所谓的MVC模式,然后使用H5 history API,所有的页面都会加载initilize.js脚本文件:

javascript/**
 * Created by jasonborn on 14/11/20.
 */

window.onload = function() {
  var title = document.querySelector(''#title'');
  var content = document.querySelector(''#content'');
  var node = document.querySelector(''#node'');
  var javascript = document.querySelector(''#javascript'');
  var resolveContent = function(target) {
    switch (true) {
      case target === ''/'':
            target = ''/articles/index.json'';
            break;
      case /node/.test(target):
            target = ''/articles/node.json'';
            break;
      case /javascript/.test(target):
            target = ''/articles/javascript.json'';
            break;
    }

    superagent
        .get(target)
        .end(function(err, res) {
          title.textContent = res.body.title;
          content.textContent = res.body.content;
        })
  };

  window.history.replaceState({
    "target": window.location.pathname
  }, null, ''./'');

  window.onpopstate = function(event) {
    resolveContent(event.state.target);
  };

  node.addEventListener(''click'', function(event) {
    event.preventDefault();

    window.history.pushState({
      "target": "/node/"
    }, null, ''/node/'');

    resolveContent(''/node/'');

    window.onpopstate = function(event) {
      resolveContent(event.state.target);
    };
  });

  javascript.addEventListener(''click'', function(event) {
    event.preventDefault();

    window.history.pushState({
      "target": "/javascript/"
    }, null, ''/javascript/'');

    resolveContent(''/javascript/'');

    window.onpopstate = function(event) {
      resolveContent(event.state.target);
    };
  });
};

pushState修改地址记录,onpopstate表示通过后退方式退回pushState后的路径时,执行何种操作。这里是通过Ajax请求拉取数据,然后呈现数据(较大型项目可能会使用前后端模板引擎来渲染,例如x-template)。

Angularjs $location html5mode

开启html5mode,通过config来配置即可。

javascriptangular.module(''shuffleApp'', [''ngRoute'', ''ngSanitize''])
    .config([''$routeProvider'', ''$locationProvider'', function($routeProvider, $locationProvider) {
        $routeProvider
            .when(''/love'', {
                template:''<a href="/hate">To hate</a><p ng-bind="love"></p>'',
                controller: ''LoveCtrl''
            })
            .when(''/hate'', {
                template: ''<a href="/love">To love</a><p ng-bind="hate"></p>'',
                controller: ''HateCtrl''
            })
            .otherwise(''/love'');
        $locationProvider.html5Mode(true);
    }]);

上述配置,在加载入口文件之后,地址栏会变为http://hostname/love,而不是http://hostname/#/love。但是存在一个问题,后者可以直接访问,前者也许能访问,也许不能,但不会出现预期的结果,典型结果就是NG中文API站,每次向在新页面打开某个链接,结果就是华丽丽的报错,例如:http://docs.angularjs.cn/api,所以需要做重定向:

javascriptapp.get(''/love'', function(req, res) {
    res.sendFile(path.join(__dirname, ''static/index.html''));
});

具体的重定向上会导致路径设计上的一些问题,所以需要注意。

联系方式

QQ: 491229492
github: https://github.com/bornkiller

AngularJS HTML5Mode

AngularJS HTML5Mode

我在我的角应用程序中使用HTML5模式来关闭hashbangs,这是因为我将为我的用户提供以下URL:

http://myapp.com/nicklewis

而不是:

http://myapp.com#/nicklewis

后者有效,但它不适合虚荣URL.

由于没有编写我自己的NodeJS应用程序来解决这个问题,Firebase中是否可以使用或不使用?

Firebase刚刚有一个包含此功能的最新更新.您可以在firebase.json中使用它:
"rewrites": [ {
    "source": "**","destination": "/index.html"
} ]

这是他们的文档中使用的代码示例,并将任何未找到的目录或文件发送回index.html.

请务必注意,您需要将firebase部署工具更新为version 1.1.0 or higher才能使其正常工作:

$npm update -g firebase-tools

根据您的权限,您可能需要使用“sudo”.

您可以在此处阅读文档:https://www.firebase.com/docs/hosting/guide/url-redirects-rewrites.html

您可以在此处阅读有关更新firebase工具的信息:https://www.firebase.com/docs/hosting/guide/command-line-tool.html

AngularJS html5mode 与第三方库冲突

AngularJS html5mode 与第三方库冲突

如何解决AngularJS html5mode 与第三方库冲突?

我的 Angular 1.8.x 路由出现问题。

在我的 angularapp.js 文件中,我启用了 html5mode,例如:

$locationProvider.html5Mode(true);

我的 NodeJS 应用执行以下操作:

module.exports = function(express,app){
    var router = express.Router();
    
    router.get(''/*'',function(req,res){
        res.render(''index.html'');
    });

    app.use(''/'',router);
};

但是,我确实遇到了第三方库 - Snipcart 的问题。应该做的是将电子商务功能包含到前端应用程序中。但是,Snipcart 的“结帐”按钮链接到一个带有 # 的 URL,并且 Snipcart 库不起作用(不去结帐,似乎对我所在的当前页面执行了几次循环)。

我的问题很简单 - 我该如何解决这个问题?不幸的是,html5mode 是必须的,但我还需要能够支持带有 # 的链接。

提前致谢!

解决方法

我不知道这是否可以归类为答案,但我与 Snipcart 交谈过,但无法让它发挥作用。我想 Angular 1 确实是那么旧

,

您也可以考虑在您的项目中使用没有主题标签的网址

http://joeljoseph.net/angularjs-remove-hash-from-url/

关于页面刷新时使用Angular JS ui.router html5Modetrue配置Amazon S3静态站点的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Angular1位置html5mode等效于angular2、Angularjs $location html5mode浅析、AngularJS HTML5Mode、AngularJS html5mode 与第三方库冲突等相关知识的信息别忘了在本站进行查找喔。

本文标签: