Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如何设置<;基地>;grunt构建的元素?_Angularjs_Gruntjs - Fatal编程技术网

Angularjs 如何设置<;基地>;grunt构建的元素?

Angularjs 如何设置<;基地>;grunt构建的元素?,angularjs,gruntjs,Angularjs,Gruntjs,我正在构建的最后一个应用程序将位于服务器上的/子目录 将angularjs的html5mode设置为true,这样我就失去了#符号,效果很好。但我也需要设置 <base href="/subdirectory/"></base> 在开发过程中,我不能将“base”元素设置为运行grunt-service时的状态,因为服务器找不到大多数文件 当我进行grunt构建时,开发并设置“base”元素的最佳方法是什么 谢谢。用于内部头部标签 请。我避免使用base标记。要解

我正在构建的最后一个应用程序将位于服务器上的/子目录

将angularjs的html5mode设置为true,这样我就失去了#符号,效果很好。但我也需要设置

<base href="/subdirectory/"></base>

在开发过程中,我不能将“base”元素设置为运行grunt-service时的状态,因为服务器找不到大多数文件

当我进行grunt构建时,开发并设置“base”元素的最佳方法是什么


谢谢。

用于内部头部标签


请。我避免使用base标记。要解决您(我也有)的问题,请做两件事:

  • 在index.html中,指定所有js/css路径作为index.html路径的关系,这样它在prod/dev中都可以工作

  • 编写一个http拦截器,在加载html/api调用时将基附加到url

    myapp.factory('MyHttpInterceptor',[ "$log", function ($log) {
    
        var requestInterceptor = function (config) {
            var prefix = document.location.pathname.substring(0, document.location.pathname.length - 1);
            if (config.url.indexOf(".html") !== -1) {
                config.url = prefix + config.url;
            }
            if (config.url.indexOf("/api/") !== -1) {
                config.url = prefix + config.url;
            }
            return config || $q.when(config);
        };
    
        return {
            request: requestInterceptor,
        };
    }
      ]);
    

  • 我使用了grunt任务
    usemin

    index.html

    <!-- build:base /subdirectory/ -->
    <base href="">
    <!-- endbuild -->
    
    
    
    grunfile.js

    usemin: {
        html: ['<%= yeoman.dist %>/{,*/}*.html'],
        css: ['<%= yeoman.dist %>/styles/{,*/}*.css'],
        options: {
            dirs: ['<%= yeoman.dist %>'],
            blockReplacements: {
                base: function (block) {
                    return ['<base href="', block.dest, '">'].join('');
                }
            }
        }
    }
    
    usemin:{
    html:['/{,*/}*.html'],
    css:['/styles/{,*/}*.css'],
    选项:{
    目录:[''],
    区块替换:{
    基本:功能(块){
    return['')。join('');
    }
    }
    }
    }
    

    基本上,我定义了一个名为
    base
    的自定义构建块,通过这种方式,它在开发过程中(或您在那里设置的任何内容)和生产过程中使用了来自构建块配置的值。

    是的,谢谢,我知道在哪里放置“base”标记。在开发过程中,我无法将其放在那里,因为运行grunt serve无法加载应用程序。@11您能帮我解决这个问题吗?我无法在dist文件夹的index.html中添加以下内容:在标记之后
    usemin: {
        html: ['<%= yeoman.dist %>/{,*/}*.html'],
        css: ['<%= yeoman.dist %>/styles/{,*/}*.css'],
        options: {
            dirs: ['<%= yeoman.dist %>'],
            blockReplacements: {
                base: function (block) {
                    return ['<base href="', block.dest, '">'].join('');
                }
            }
        }
    }