在yeoman angular应用程序中使用bower时,我可以更改呈现的路径吗?

sco*_*ttm 3 angularjs gruntjs bower yeoman-generator

我在我的角应用程序中使用html5mode,但是当我运行bower install时grunt,它将路径呈现为

<script src="bower_components/...

我可以改成这个吗?

<script src="/bower_components/...?

我已经<base href="/">在我的<head/>部分添加了一个标签,但是当我的页面尝试加载javascript时,他们仍然在寻找相对的网址.

因此对于/bower_components/script.js目前呈现 为的脚本<script src="bower_components/script.js"></script>,

启用时/login,页面会尝试加载/login/bower_components/script.js

编辑 按照@ sharpper的建议,我在下面添加了代码块,最后在Gruntfile.js中使用了这个:

// Automatically inject Bower components into the app
'bower-install': {
  app: {
    html: '<%= yeoman.app %>/index.html',
    ignorePath: '<%= yeoman.app %>/',
    fileTypes: {
      html: {
        block: /(([\s\t]*)<!--\s*bower:*(\S*)\s*-->)(\n|\r|.)*?(<!--\s*endbower\s*-->)/gi,
        detect: {
          js: /<script.*src=['"](.+)['"]>/gi,
          css: /<link.*href=['"](.+)['"]/gi
        },
        replace: {
          js: '<script src="/{{filePath}}"></script>', // <-- Change this line
          css: '<link rel="/stylesheet" href="{{filePath}}" />'
        }
      },
      yml: {
        block: /(([\s\t]*)#\s*bower:*(\S*)\s*)(\n|\r|.)*?(#\s*endbower\s*)/gi,
        detect: {
          js: /-\s(.+)/gi,
          css: /-\s(.+)/gi
        },
        replace: {
          js: '- {{filePath}}',
          css: '- {{filePath}}'
        }
      }
    }
  }
},
Run Code Online (Sandbox Code Playgroud)

它仍然可以正确编译,但文件呈现方式相同.

Sam*_*hen 11

首先,确保你的bower-install在最新版本:v0.8.0(yeoman当前正在使用v0.7.0),然后下面的代码应该工作:

     'bower-install': {
         app: {
            src: ['<%= yeoman.app %>/index.html'],
            ignorePath: '<%= yeoman.app %>/',
            fileTypes: {
                html: {
                    replace: {
                        js: '<script src="/{{filePath}}"></script>',
                        css: '<link rel="stylesheet" href="/{{filePath}}" />'
                    }
                }
            }
        }
     }
Run Code Online (Sandbox Code Playgroud)

如果您不想更新或此方法不起作用,请尝试此技巧:更改ignorePath: '<%= yeoman.app %>/',为ignorePath: '<%= yeoman.app %>',,然后它应该适合您的目的.