我是Angular的新手,最近我开始从事一个简单的个人SPA项目。它是使用angular cli创建的,文件夹结构没什么特别之处:
- myApp
----e2e
----node_modules
----src
--------app
------------ main app module with two .ts components and html for each of them
...
当我使用ng serve
时,一切正常:我可以在localhost:4200
上看到我的组件,Angular的功能可以作为一种魅力。
但是,当我运行ng build
并从新创建的dist
文件夹中打开index.html
时,我根本无法看到我的组件。
index.html
文件如下:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>RoutingDemo</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root></app-root>
<script type="text/javascript" src="runtime.js"></script><script type="text/javascript" src="polyfills.js"></script><script type="text/javascript" src="styles.js"></script><script type="text/javascript" src="vendor.js"></script><script type="text/javascript" src="main.js"></script></body>
</html>
我在控制台中没有丢失文件的错误,并且index.html成功读取了所有JS和CSS文件。
有人可以解释为什么我没有看到组件?
所以你需要为你的页面提供服务,最简单的方法是在你的npm i -g http-server
文件夹中执行http-server
和从命令lint运行dist
,然后你就能看到你的页面了