我有一个使用Angular 1.5组件的小型Web应用程序。当我启动服务器时,浏览器会将我重定向到http://127.0.0.1:50001/welcome,这是预期的,并显示欢迎页面。例如,如果要创建一个新用户,请单击链接,URL更改为http://127.0.0.1:50001/welcome/newUser,然后出现新用户表格。
当我尝试直接访问URL(或刷新页面)时,就会发生此问题-该页面根本无法加载。控制台中没有出现错误-没有任何反应。
我的配置如下:
根成分:
.component('webGuiComponent', {
template: '<ng-outlet></ng-outlet>',
$routeConfig: [
{ path: '/welcome/...', name: 'Welcome', component: 'welcomeComponent', useAsDefault: true }
]
})
.value('$routerRootComponent', 'webGuiComponent');
欢迎组件:
.component('welcomeComponent', {
template: '<header></header><ng-outlet></ng-outlet>',
$routeConfig: [
{ path: '/', name: 'Index', component: 'indexComponent', useAsDefault: true },
{ path: '/newUser', name: 'NewUser', component: 'newUser' }
]
})
.component('indexComponent', {
templateUrl: '/app/components/welcome/index.html'
});
新的用户组件:
.component('newUser', {
controller: 'userController',
templateUrl: '/app/components/user/new.html'
})
导航链接使用标准的ng-links:
<a class="navbar-brand" ng-link="['/Welcome/Index']">Web GUI</a>
<a class="navbar-brand" ng-link="['/Welcome/NewUser']">Sign Up</a>
有谁知道为什么通过ng-link完成导航而不是直接访问URL时导航有效?
您必须为可能在客户端定义的路由的所有路径添加服务器端路由,这将呈现您的主页。实现方式取决于您使用的服务器端技术。
或者您必须使用基于#的网址
解释
当您没有基于#的URL时,浏览器会转到服务器以获取资源,但找不到任何内容。因此,当您定义服务器端路由时,它将服务于主页,然后进行客户端路由。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句