gpt4 book ai didi

grails - 刷新浏览器会绕过基于routerProvider的URL的 Angular

转载 作者:行者123 更新时间:2023-12-02 14:25:07 24 4
gpt4 key购买 nike

我有一个grails应用程序支持angularjs前端。它们被部署为单个WAR。我已经从应用程序中删除了上下文路径,以便它可以在http://localhost:8080上运行。

我有一个文章列表,并且我具有$routeProvider设置,用于将/重定向到/articles,此时 Controller 将接管并通过$http提取列表。很标准的东西。

最初,我使用默认的位置提供程序配置,因为URL中使用了哈希(#)。我已经通过更改了

$locationProvider.html5Mode(true);

一切仍然有效。但是,如果直接在地址栏中更改URL并按Enter,或者在/ articles处刷新浏览器时,服务器端将接管,而我将文章列表作为json获取。没有 Angular 。我知道为什么会发生这种情况,现在我所做的事情在服务器上检测到一个非ajax请求,并发出了对 /的重定向,这将使angular投入使用。

我想知道这是否正确。还是我还有其他可以做的更好的做法。

最佳答案

重定向是正确的解决方案。

我能够使用url映射使其工作。到目前为止,它是可行的:-)

我开始是这样的:

"/**" (controller: 'app', action: 'index') 

其中app / index为 Angular 应用页面。但这也将匹配其他所有内容(例如/ $ controller / $ action)。我必须显式地将每个$ controller / $ action映射到正确的 Controller 。不太好... ;-)

为了解决这个问题,我在所有uris前面添加 /client来表示 Angular 路线,在 /server前面添加grails uris。这使url映射变得容易,并且有助于区分 Angular 路径与模板uris等。

我最终的网址映射如下所示:
class UrlMappings {
static excludes = [
"/lib/**",
"/css/**",
"/js/**"
]

static mappings = {
// - all client uris (routes) will start with '/client/',
// - all server uris (load) will start with '/server/'

// redirect /$appName/ to /$appName/client
"/" (controller: 'redirect', action: 'redirectTo') {
to = '/client/'
permanent = true
}

// redirect any angular route
"/client/**" (controller: 'app', action: 'index')


// standard controller/action mapping
"/server/$controller/$action/$id?" {
constraints {
}
}
}
}

您不能直接在url映射中重定向,所以我使用一个简单的 Controller :
class RedirectController {
def redirectTo () {
redirect (uri: params.to, permanent: params.permanent)
}
}

路由条目如下所示:
$routeProvider.when ('/client/login', {templateUrl: './server/security/login'});

关于grails - 刷新浏览器会绕过基于routerProvider的URL的 Angular ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17893770/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com