加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular_base url和gh-pages上的相对路径

发布时间:2020-12-17 17:46:25 所属栏目:安全 来源:网络整理
导读:我正在尝试在gh-pages上部署我的项目站点,它是带有webpack的angular2应用程序,我将基本URL设置为我的repo名称,除了模板中的相对路径外,一切都正常加载,这就是我的意思 import {Component} from '@angular/core';@Component({ selector: 'header',template: `
我正在尝试在gh-pages上部署我的项目站点,它是带有webpack的angular2应用程序,我将基本URL设置为我的repo名称,除了模板中的相对路径外,一切都正常加载,这就是我的意思

import {Component} from '@angular/core';

@Component({
  selector: 'header',template: `
      <img class="fb-logo" [src]="fbLogo"/>
      <img class="ang-logo" [src]="angularLogo"/>
  `
})

export class Header{

    angularLogo = '../../assets/img/angular-logo.png';
    fbLogo = '../../assets/img/share/facebook.svg';
}

这适用于本地dev,但是当我将它推送到gh-pages分支时它会给404,因为它试图从根服务器http://myAcc.github.io/assets/img/fbLogo.svg而不是http获取它://myAcc.github.io/myRepo/assets/img/fbLogo.svg

我无法弄清楚如何解决它,所以我尝试使用它作为inline-svg使用require

angularLogo = require('../../assets/img/angular-logo.png');
fbLogo = require('../../assets/img/share/facebook.svg');

它在本地(使用xss警告)工作正常,但当我将它部署到gh-pages时它拒绝在https上运行.

我该如何解决这个问题?

解决方法

问题是你的dev和你的gh页面有不同的根级别,你的dev root url必须是这样的:http:// localhost:port而你的gh-page root是http://myAcc.github.io那是为什么你的相对图标不起作用.

我猜你有不同的webpack配置,将你的生产配置改为这样的:

output: {
  path: 'something',filename: 'something.bundle.js',publicPath: '/myRepo/' // this will add /myRepo/ to all your assets
}

这是关于publicPath的快速解释:

output.publicPath

The publicPath specifies the public URL address of the output files
when referenced in a browser. For loaders that embed <script> or
<link> tags or reference assets like images,publicPath is used as the
href or url() to the file when it’s different than their location on
disk (as specified by path). This can be helpful when you want to host
some or all output files on a different domain or on a CDN. The
Webpack Dev Server also uses this to determine the path where the
output files are expected to be served from. As with path you can use
the [hash] substitution for a better caching profile.

您可以找到有关publicPath here的更多信息

更新:

output.publicPathonly可以处理已经声明的资产,因为您动态指定了img源它将无法工作.您可以使用文件加载器为您完成此操作,只需将您的加载器更改为:

{
  test: /.(jpg|png|gif)$/,loader: 'file?name=/myRepo/[name].[ext]'
},

因此,只要您在代码中需要jpg | png | gif,就会添加字符串/ myRepo /.

另一个解决方案是创建自定义管道,因为您正在使用angular2-webpack-starter,您的构建过程会将您的环境导出到可变的ENV,以便您可以将其用于自定义管道,如下所示:

import { Pipe,PipeTransform } from '@angular/core';

@Pipe({name: 'pathResolver'})
export class PathResolverPipe implements PipeTransform {
  transform(path: string): string {

    return ENV === 'production' ? path.replace(/assets/i,'myRepo/assets') : path;
  }
}

并在您的组件上使用它,如下所示:

import {Component} from '@angular/core';
import { PathResolverPipe } from 'path/to/pipe';

@Component({
  selector: 'header',pipes: [PathResolverPipe],template: `
      <img class="fb-logo" [src]="fbLogo | pathResolver"/>
      <img class="ang-logo" [src]="angularLogo | pathResolver"/>
  `
})

export class Header{

    angularLogo = '../../assets/img/angular-logo.png';
    fbLogo = '../../assets/img/share/facebook.svg';
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读