如何启动'ng serve -o'来启动index.html以外的页面?

efl*_*lat 3 angular-cli

我们的角度应用程序将在生产时嵌入到iframe中.在开发中,我们有一个文件host.html,它有一个iframe,其src是angular app的索引页面.我们可以'服务',然后手动调出host.html,它工作正常.

但是,'ng serve -o'想要打开app索引文件; 我如何让它打开host.html而不是?

Joh*_*ohn 7

因此我不认为angular cli支持使用该-o选项打开任意页面,但您可以创建自己的npm脚本来执行您想要的操作.

尝试打开package.json并编辑该scripts部分以更新start脚本(mac版本):

"scripts": {
    "ng": "ng",

    // where localhost:4200 is your dev host and port, respectively
    "start": "ng serve && open http://localhost:4200/host.html",

    "build": "ng build",
    ...
  },
Run Code Online (Sandbox Code Playgroud)

根据您使用的平台,脚本需要以不同的方式编写:

// Windows
"start":"ng serve & start http://localhost:4200/host.html"

// Mac
"start":"ng serve && open http://localhost:4200/host.html"

// Linux 
"start":"ng serve && xdg-open http://localhost:4200/host.html"
Run Code Online (Sandbox Code Playgroud)

然后,使用,而不是使用ng serve启动您的应用程序npm start.

参考:针对平台特定命令使用此SO答案open.

  • 这是行不通的,因为“ngserve”永远不会完成,所以“&start...”永远不会执行。至少这是我在 Windows 上的经验。 (4认同)