angular - 如何获取 webpack publicPath

标签 angular webpack

我正在使用以下代码向 MdIconRegistry 添加图标:

constructor(iconRegistry: MdIconRegistry, sanitizer: DomSanitizer) {
  iconRegistry.addSvgIconSet(
    sanitizer.bypassSecurityTrustResourceUrl('/assets/svg-sprite-action-symbol.svg'));
  iconRegistry.addSvgIconSet(
    sanitizer.bypassSecurityTrustResourceUrl('/assets/svg-sprite-content-symbol.svg'));
  iconRegistry.addSvgIconSet(
    sanitizer.bypassSecurityTrustResourceUrl('/assets/svg-sprite-navigation-symbol.svg'));
}

不幸的是,这在使用 ng [build|serve] --deploy-url=/public/ 时会中断。 (在生产中,我想将 ng build 输出部署到 CDN)

有没有一种方法可以从我的 Angular 组件访问 webpack publicPath 变量,以便我可以指定 svg 图标的 url 相对于此?

我查看了ng build输出,我可以看到变量设置在 inline.bundle.js ,但我不确定如何正确访问它。
/******/    __webpack_require__.p = "/public/";

最佳答案

您可以从 __webpack_public_path__ 获取全局变量。

关于angular - 如何获取 webpack publicPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45435284/

相关文章:

javascript - Angular 2 仅从 root/app 文件夹加载组件

javascript - Angular 2 : Dynamic style two color div on the basis of HTML table column values ratio percentage

javascript - 需要用 gulp 延迟加载 ES6 模块

typescript - 如何用Webpack在Typescript中打包大型框架

javascript - 如何使用 Angular 1.x 和 Typescript 设置 Webpack 以使用 Angular 模板(ng-include)?

javascript - Angular Input() 在提供默认值后采用 undefined

angular - 无法在环境上下文中声明访问器

Angular 6 - 根据环境提供不同的索引文件

javascript - 是否可以从 webpack 生成的缩小的 JS 文件生成原始文件

node.js - 在调试时拥有可用的第三方 JavaScript 库的源代码