静态文件的相对路径,如images / css / js等

时间:2016-09-30 12:43:49

标签: css image angular angular2-routing static-files

我的文件夹结构为/src/app/some-module/some-componentsrc/public/images/user.png。现在,当我想要在我的某个组件中显示图像时,我必须将路径设为../../../public/images/user.png,一旦图像数量增加,这似乎过于天真和浪费。

我们是否在angular2中有一个路由机制或相对路径来提供静态文件。我正在使用Angular2和webpack。

2 个答案:

答案 0 :(得分:5)

这取决于public class CoinFlip { static String side = ""; static String user = ""; static String winner = ""; public static void main(String args[]) { String coin; greeting(); do { user=IO.getString("Would you like: (H)- Heads or (T)- Tails"); coin=flip(); winner=compare(user,coin); output(user,coin,winner); }while( repeat() ); } public static void greeting() { System.out.println("In a second, choose heads or tails. A coin flip will show and a winner will be shown."); } public static void flip() { int random = (int) Math.floor(Math.random() * 2); if (random = 0) side = "H"; else if (random = 1) side = "T"; } public static void compare(String args[]) { if (user = side) winner = "Winner"; else winner = "Loser"; } public static void output() { System.out.println(side); System.out.println(winner); } public static void repeat() { String again; if (again = "yes") main(); else if (again = "no") System.out.println("Thanks for playing!"); } } 的外观。

例如,如果你有

base href

无论您的组件模板在何处,您都可以轻松使用。

<base href="/src/">

希望这会有所帮助!!

答案 1 :(得分:1)

如果您使用WebPack,它会在构建期间处理路径。 <img src="...">需要指向项目中文件的物理位置,相对于使用它的模板。

例如,在我的情况下,没有WebPack,它将是

<img src="img/rhamt-square-248.png" width="172">

因为img/直接位于应用根目录下。

然而,对于WebPack,这有效:

<img src="../../../img/rhamt-square-248.png" width="172">

否则您会收到编译错误,例如:

   ./src/app/misc/about.component.html中的错误   找不到模块:错误:无法解决&#39; ./ img / rhamt-square-248.png&#39;在&#39; / home / ondra / work / Migration / windup-web / ui / src / main / webapp / src / app / misc&#39;
   @ ./src/app/misc/about.component.html 1:402-439
   @ ./src/app/misc/about.component.ts
   @ ./src/app/app.module.ts
   @ ./src/main.ts

相关问题