如果用户输入了错误的网址,我希望将用户带到找不到页面页面,因此我有 $urlRouterProvider.otherwise("/not-found")
设置来处理这个问题。
但是,现在当我启动应用程序时,它会从未找到的页面开始,这并不完全有帮助。 如何在主页启动应用程序,然后从那时起,如果用户输入任何错误的路由,他们就会点击 .otherwise()
?
我正在查看$stateChangeStart
,但这似乎不可行。我就是这么做的
if (fromState.name === "" && toState.name !== "not-found") {
event.preventDefault();
$state.go("home");
}
认为在应用程序的入口点,.name
将为空白,我可以重定向到主页。但是,由于其他设置,toState.name
始终等于“not-found”。
编辑
“使用严格”;
class RoutingConfig {
static $inject = ["$stateProvider", "$urlRouterProvider", "$locationProvider"];
constructor($stateProvider, $urlRouterProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$stateProvider
.state("home", {
url: "/home",
templateUrl: "components/home/home.html",
controller: "HomeController",
controllerAs: "vm"
})
.state("not-found", {
url: "/not-found",
templateUrl: "components/page-not-found/page-not-found.html"
});
var useHome = true;
$urlRouterProvider.otherwise(() => {
if (useHome) {
useHome = false;
return "/home";
}
return "/not-found";
});
}
}
最佳答案
如果第一次触摸应该转到“未找到”之外的其他地方,我们可以使用某种带有函数
的.otherwise()
和一些简单的 bool
开关:
var useHome = true;
$urlRouterProvider.otherwise(function(){
if(useHome){
useHome = false;
return "/home";
}
return "/notFound";
});
关于angularjs - 使用 ui-router,如果 .otherwise() 指向未找到的页面,如何设置起始页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37466883/