这是 Grunt 文件中 livereload block 的样子:
livereload: {
options: {
open: true,
middleware: function(connect, options, middleware) {
var optBase = typeof options.base === "string"
? [options.base]
: options.base;
return [
[
require("connect-modrewrite")(["!(\\..+)$ / [L]"])
].concat(
optBase.map(function(path) { return connect.static(path); })
),
connect.static(".tmp"),
connect().use("/bower_components", connect.static("./bower_components")),
connect().use("/app/styles", connect.static("./app/styles")),
connect.static(appConfig.app)
];
}
}
}
但是,如果我的 URL 有一个“。” (句号)在其中,Grunt 无法重新加载页面。我在我的 Angular 应用程序中使用 HTML5 模式,效果很好。
请问我可以知道是哪一部分吗
[
require("connect-modrewrite")(["!(\\..+)$ / [L]"])
].concat(
optBase.map(function (path) { return connect.static(path); })
)
导致它失败,我该如何解决这个问题?
注意:它仅在页面重新加载时失败。我第一次访问它工作的路线时,如果我点击刷新它会失败。
最佳答案
Could I please know what part of [this snippet] is causing it to fail and how do I fix this?
connect-modrewrite
此处的规则似乎仅用于重写非静态 Assets URL。[
require("connect-modrewrite")(["!(\\..+)$ / [L]"])
].concat(
optBase.map(function (path) { return connect.static(path); })
)
这里传入的规则,"!(\\..+)$ / [L]"
, 是 inverted URL matching rule将所有不包含句点和一个或多个字符的 URL 重写回基本 URL (/
)。因此,如果您的路线包含一个句点,它们将不会被重写......这就是为什么当实时重新加载运行时它会失败。解决此问题的一个建议是更明确地避免重写对静态 Assets 的请求:
require("connect-modrewrite")([
'!\\.html|\\.js|\\.svg|\\.css|'
+ '\\.scss.*|\\.woff.*|\\.gif.*|\\.png$ '
// etc...
+ '/ [L]'
])
请注意,此规则也以感叹号 (!
) 开头。它不仅仅依赖于句点的存在,它更明确地针对不包含某些文件扩展名的 URL - 以及句点! (:注:code for the suggestion was found on searchcode.com here
我希望这有帮助! HMU 有任何问题、更正或实际情况。
关于javascript - 重新加载页面时在 Angular 应用程序中重写 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47493760/