我有一个mean-stack
项目。在我的 index.js
中,我最终
router.get('*', function(req, res) {
res.sendfile('./views/index.html');
})
module.exports = router;
现在我想通过 ./views/addinIndex.html
处理与 https://localhost:3000/1/addin/*
匹配的所有网页,我在其中期望引用不同的外部文件并定义不同的模块。因此,我添加另一个 router.get
:
router.get('/1/addin/*', function (req, res) {
res.sendfile('./views/addinIndex.html')
})
router.get('*', function(req, res) {
res.sendfile('./views/index.html');
})
module.exports = router;
这是addinIndex.html
:
<html>
<head>
<script src="https://code.jquery.com/jquery.min.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.3.2/angular-ui-router.js"></script>
<script>
jQuery(document).ready(function () {
angular.bootstrap(document, ['app']);
console.log("bootstrapped")
})
app = angular.module('app', ['ui.router'])
app.config(['$stateProvider', function($stateProvider) {
$stateProvider
.state('addinHome1', {
url: '/1/addin/home',
template: 'addinHome1'
})
.state('addinHome2', {
url: '/addin/home',
template: 'addinHome2'
})
.state('addinHome3', {
url: '/home',
template: 'addinHome3'
})
}])
</script>
</head>
<body>
abcde
<ui-view ng-cloak></ui-view>
</body>
</html>
但是,当我运行 https://localhost:3000/1/addin/home
时,它显示 abcde
而没有其他内容,而 bootstrapped
在控制台中打印。
有人知道这是怎么回事吗?鉴于我已经有一个可用的 index.html
,我可以像这样定义一个额外的 addinIndex.html
吗?
最佳答案
是的,可以根据路线提供不同的索引。
但为此,您必须从网址中删除 #
。
为此,请按照以下步骤操作。
我们将使用 $locationProvider 模块并将 html5Mode 设置为 true。
// use the HTML5 History API $locationProvider.html5Mode(true);
要使用相对链接来链接您的应用程序,您需要在文档中设置 。在您的 Angular 配置中添加以下代码。
<base href="/">
关于javascript - 在1个项目中制作2个模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48055290/