我的Angular应用程序托管在服务器上,应用程序的URL为http://xyz.domain.in(示例URL)。现在我在其中创建了一个子目录来托管另一个应用程序。但是当我尝试访问路径http://xyz.domain.in/subfolder时,在控制台中会出现以下错误:
错误:无法匹配任何路由。 URL段:'subfolder'
子文件夹中应用程序的基本href是
<base href="./subfolder">
如何完成这个任务?我的Angular应用程序托管在服务器上,应用程序的URL为http://xyz.domain.in(示例URL)。现在我在其中创建了一个子目录来托管另一个应用程序。但是当我尝试访问路径http://xyz.domain.in/subfolder时,在控制台中会出现以下错误:
错误:无法匹配任何路由。 URL段:'subfolder'
子文件夹中应用程序的基本href是
<base href="./subfolder">
如何完成这个任务?启动构建时,您必须指定子文件夹的路径:
ng build --base-href=/subfolder/
如果您在部署时使用的是子文件夹,则无需提及子文件夹名称,只需提及href="./"。
构建到子文件夹中:
ng build --prod --output-path="dist/subfolder" --deployUrl="subfolder/"
pathMatch
:{path: 'url', component: YourComponent, pathMatch: 'subfolder'},
在子目录上正确设置Angular应用程序的步骤如下:
baseHref
属性。例如:...
build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"baseHref": "/subdirectory1/subdirectory2",
<base href="/subdirectory1/subdirectory2">
确保在您的应用程序路由模块的ExtraOption
中未设置useHash
(因为默认值为false)
更新图像和样式链接。任何绝对图像链接都应该变成相对链接
例如:从:src="/assets/img/pic.jpg"
变成:src="assets/img/pic.jpg"
scss 例子:
从:background-image: url('/assets/img/pic.jpg
变成:background-image: url('../../../assets/img/pic.jpg