angularjs – 在yeoman角度应用程序中使用bower时,我可以更改渲染的路径吗?

前端之家收集整理的这篇文章主要介绍了angularjs – 在yeoman角度应用程序中使用bower时,我可以更改渲染的路径吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的角度应用程序中使用 html5mode,但是当我使用grunt运行bower install时,它将路径呈现为

< script src =“bower_components / ... 我可以改成这个吗? < script src =“/ bower_components / ...? 我添加了< base href =“/”>标记到我的< head />部分,但当我的网页尝试加载JavaScript时,他们仍然在寻找相对的网址.

因此对于/bower_components/script.js中的脚本,当前呈现为
< script src =“bower_components / script.js”>< / script>,

当打开/登录时,页面尝试加载/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}}'
        }
      }
    }
  }
},

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

解决方法

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

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

如果您不想更新或此方法不起作用,请尝试以下方法:       将ignorePath:’<%= yeoman.app%> /’更改为ignorePath:’<%= yeoman.app%>‘,然后它应该适用于您的目的.

猜你在找的Angularjs相关文章