我在我的角度应用程序中使用 html5mode,但是当我运行bower install
时grunt
,它会将路径呈现为
<script src="bower_components/...
我可以将其更改为
<script src="/bower_components/...
?
我在<base href="/">
我的部分中添加了一个标签<head/>
,但是当我的页面尝试加载 javascript 时,它们仍在寻找相对 url。
/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}}'
}
}
}
}
},
它仍然可以正确编译,但文件呈现相同。