javascript - 在1个项目中制作2个模块

标签 javascript angularjs express angular-ui-router mean-stack

我有一个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 吗?

最佳答案

是的,可以根据路线提供不同的索引。 但为此,您必须从网址中删除 #

为此,请按照以下步骤操作。

  1. 我们将使用 $locationProvider 模块并将 html5Mode 设置为 true。

    // use the HTML5 History API
    $locationProvider.html5Mode(true);
    
  2. 要使用相对链接来链接您的应用程序,您需要在文档中设置 。在您的 Angular 配置中添加以下代码。

    <base href="/">
    

关于javascript - 在1个项目中制作2个模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48055290/

相关文章:

javascript - 属性符合我的值(value)

javascript - 在 html 中包含 javascript 的最佳实践

javascript - 如何从弹出选项显示 Ionic 弹出窗口?

javascript - AngularJS无法读取未定义的属性 'otherwise'

node.js - 如何将 Sequelize 查询的输出传递给另一个将使用 express 返回 JSON 对象的函数?

javascript - 如何检测contenteditable中的文本溢出

javascript - 通过特定应用程序获取轨道时,标签过滤似乎不起作用

javascript - 带有 ExpressJS 路由器的 CRUD

node.js - sequelize 动态查询参数

javascript - 来自 Amazon S3 的 res.download 文件