Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角5生产问题_Angular_Deployment_Build - Fatal编程技术网

Angular 角5生产问题

Angular 角5生产问题,angular,deployment,build,Angular,Deployment,Build,我正在尝试使用以下命令在nginx服务器上部署Angular 5应用程序 ng build --env=prod 但当我访问我的网站时,供应商文件上总是有一个错误 当我使用命令在/test这样的文件夹中构建应用程序时,它运行良好:ngbuild--env=prod--bh/test/ 我真的迷路了。这是nginx的问题吗 非常感谢。您没有使用-bh选项指定文件夹。您可以指定基本href 当您想从根目录访问时,只需使用-bh/。如果有预定义的基本路径,请检查index.html <base

我正在尝试使用以下命令在nginx服务器上部署Angular 5应用程序

ng build --env=prod
但当我访问我的网站时,供应商文件上总是有一个错误

当我使用命令在/test这样的文件夹中构建应用程序时,它运行良好:
ngbuild--env=prod--bh/test/

我真的迷路了。这是nginx的问题吗


非常感谢。

您没有使用-bh选项指定文件夹。您可以指定基本href

当您想从根目录访问时,只需使用-bh/。如果有预定义的基本路径,请检查index.html

<base href="/">

默认情况下,Angular使用PathLocationStratergy,当您执行产品构建时,如果我们没有明确指定基本href,则无法使其工作。 如果在此情况下不想指定基本href,则需要在路由中使用HashLocationStratergy

例子 forRoot(appRoutes,{useHash:true}

还有一个窍门 以下不是最佳做法 生成产品后,只需将index.html中的base href更改为

而不是

ng build --env=prod
使用


如果有人还在为angular 2/4/5 app+Nginx的生产设置而挣扎,那么以下是完整的解决方案:

假设您想在主机上部署angular应用程序:
http://example.com
和端口:
8080
注意-在您的情况下,主机和端口可能不同

确保index.html头标记中有

  • 首先,在您的机器上转到角度repo(即/home/user/helloWorld)路径

  • 然后,使用以下命令为生产服务器生成/dist:

    ng build--prod--base href

  • 现在,将/dist(即/home/user/helloWorld/dist)文件夹从计算机的angular repo复制到要承载生产服务器的远程计算机

  • 现在登录到远程服务器并添加以下nginx服务器配置

    server {
    
        listen 8080;
    
        server_name http://example.com;
    
        root /path/to/your/dist/location;
    
        # eg. root /home/admin/helloWorld/dist
    
        index index.html index.htm;
    
        location / {
    
            try_files $uri $uri/ /index.html;
    
            # This will allow you to refresh page in your angular app. Which will not give error 404.
    
        }
    
    }
    
  • 现在重新启动nginx

  • 就这样!!现在,您可以访问您的angular应用程序


  • 希望它会有所帮助。

    即使使用该命令,问题仍然是在函数体vendor.bundle.js:2:397750和Nginx Config:you在ng serve--prod中遇到同样的错误吗?我尝试使用--prod参数构建它。该网站显示正常,但FormsModule不起作用。这就是为什么我使用环境论证,我没有要求构建,而是服务:)哦,对不起。使用serve--prod,我也不能使用FormModule。并且没有创建任何文件。目的是使用VestaCP及其接口。所以我不能在那里发球。我会尽快尝试:)!你对apache有什么想法吗?它只是重定向到/index.html?
    server {
    
        listen 8080;
    
        server_name http://example.com;
    
        root /path/to/your/dist/location;
    
        # eg. root /home/admin/helloWorld/dist
    
        index index.html index.htm;
    
        location / {
    
            try_files $uri $uri/ /index.html;
    
            # This will allow you to refresh page in your angular app. Which will not give error 404.
    
        }
    
    }