【问题标题】:Angular 2 ng buildAngular 2 构建
【发布时间】:2017-02-22 21:33:34
【问题描述】:

所以我需要创建一个可以在不运行服务器的情况下工作的构建。

运行ng build。有一个dist 文件夹。

当我运行index.html 时,我遇到了类似的错误

GET file:///D:/inline.bundle.js net::ERR_FILE_NOT_FOUND

他正在我的 D:// 驱动器的根目录中搜索 js 文件,而不是在项目文件夹中

好吧,我从我的index.html 中删除了一个<base href="/"> 并得到了这个错误:

Unhandled Promise rejection: No base href set. Please provide a value for the APP_BASE_HREF token or add a base element to the document. ; Zone: <root> ; Task: Promise.then ; Value: ZoneAwareError {__zone_symbol__error: Error: No base href set. Please provide a value for the APP_BASE_HREF token or add a base element to…, __zone_symbol__stack: "Error: No base href set. Please provide a value fo…testing1/dist/vendor.bundle.js:28631:62) [<root>]", __zone_symbol__message: "No base href set. Please provide a value for the A…HREF token or add a base element to the document."} Error: No base href set. Please provide a value for the APP_BASE_HREF token or add a base element to the document.
at new PathLocationStrategy (file:///D:/angular/testing1/dist/vendor.bundle.js:66150:19) [angular]
at provideLocationStrategy (file:///D:/angular/testing1/dist/vendor.bundle.js:62944:9) [angular]
at AppModuleInjector.get (/AppModule/module.ngfactory.js:164:77) [angular]
at AppModuleInjector.get (/AppModule/module.ngfactory.js:169:84) [angular]
at AppModuleInjector.get (/AppModule/module.ngfactory.js:198:151) [angular]
at AppModuleInjector.createInternal (/AppModule/module.ngfactory.js:241:60) [angular]
at AppModuleInjector.NgModuleInjector.create (file:///D:/angular/testing1/dist/vendor.bundle.js:56323:76) [angular]
at NgModuleFactory.create (file:///D:/angular/testing1/dist/vendor.bundle.js:56291:18) [angular]
at file:///D:/angular/testing1/dist/vendor.bundle.js:38426:61 [angular]
at Object.onInvoke (file:///D:/angular/testing1/dist/vendor.bundle.js:28762:37) [angular]
at Zone.webpackJsonp.528.Zone.run (file:///D:/angular/testing1/dist/polyfills.bundle.js:2591:43) [<root> => angular]
at NgZone.run (file:///D:/angular/testing1/dist/vendor.bundle.js:28631:62) [<root>]

所以angular2router 在没有&lt;base&gt; 的情况下无法工作,而index.html 无法使用&lt;base&gt; 找到我的本地文件

我该如何解决?

【问题讨论】:

  • 只是放回 但带有 .

标签: javascript node.js angular typescript angular-cli


【解决方案1】:

将 --base-href 与适当的基数一起使用

来自 ng 帮助构建:

--base-href(字符串)(默认值:/)
别名:-bh

【讨论】:

    【解决方案2】:
    Bonjour,
    
    <div>Ma question concerne ng build d'un projet angular 2 dont voici un exemple :
    

    [我的项目的屏幕截图][1]

    Quand j'exécute la commande ng build, toutes les ressources de mon projet sont transférées dans le répertoire "dist", donc normale jusque là.
    Cependant, dans ce dossier, je constate que certains de mes fichiers media et fonts qui se trouvent dans "assets", se retrouve en double à l’extérieur de ce dossier (Voir copie écran ci-dessous).
    Par exemple, le fichier image back.jpg qui est rangé dans  ponyracing/dist/assets/img/back.jpg, se retrouve en double à l’extérieur dans ponyracing/dist/back.jpg
    
    De plus, voici la config de angular-cli.json  : 
    
    "styles": [
      "assets/css/main.css",
      "styles.css",
      ...
    ]
    
    Ma question est de savoir si ce problème de fichiers doubles est causé par mon fichier main.css externe que j’ai rangé dans ponyracing/dist/assets/css/main.css et que dans ce fichier, le path des images est relatif au dossier ponyracing/dist/assets/img ?
    
    Exemple : body { background: url( ../img/back.jpg ) };
    
    Merci pour votre aide.</div>
    

    【讨论】:

      【解决方案3】:

      添加一个“。”到你的基本href,像这样:

      <base href="./">
      

      感谢 Admir 的评论,这对我有用。

      【讨论】:

        【解决方案4】:

        在 index.html 中使用下面的脚本。

        <script>
            document.write('<base href="' + String(document.location).replace('index.html','') + '">')
        </script>
        

        让我知道它是否适合你。

        【讨论】:

          猜你喜欢
          • 2017-12-27
          • 1970-01-01
          • 2017-07-19
          • 2018-02-09
          • 1970-01-01
          • 2017-07-27
          • 2017-03-06
          • 2017-06-17
          • 1970-01-01
          相关资源
          最近更新 更多