简体   繁体   English

Angular 5路由和导航,URL错误

[英]Angular 5 routing and navigation with URL Error

I am developing a small project in Angular 5, I made the router configuration in the code, if I click the menu in navbar I can navigate between components, but if I enter a component through the URL it gives me error. 我正在Angular 5中开发一个小项目,我在代码中进行了路由器配置,如果单击导航栏中的菜单,则可以在组件之间导航,但是如果通过URL输入组件,则会出现错误。

If you enter the link by this https://fit.simpleclouder.com/ it works. 如果您通过此https://fit.simpleclouder.com/输入链接,那么它将起作用。

If you enter the link by this https://fit.simpleclouder.com/home it does not work. 如果您通过此https://fit.simpleclouder.com/home输入链接,则该链接无效。

If you enter the link by this https://fit.simpleclouder.com/benefics does not work. 如果您通过此网址输入链接,则https://fit.simpleclouder.com/benefics不起作用。

If I click through the menu on the website, it works. 如果我单击网站上的菜单,它将起作用。

In a main folder in ftp i have this project on folder "laravel/public/fit", because i have another project Laravel on main folder. 在ftp的主文件夹中,我在“ laravel / public / fit”文件夹中有此项目,因为我在主文件夹中有另一个项目Laravel。

it is not angular problem it is in your IIS or your Apache server for IIS make web.config and put 这不是角度问题,它在您的IIS或IIS的Apache服务器中,请进行web.config并放入

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="AngularJS Routes" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="/" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

in Apache make .htaccess file and put 在Apache中制作.htaccess文件并放入

RewriteEngine On  
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]

# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html

have fun and edit the routing as you need 玩得开心,并根据需要编辑路由

Instead of https://fit.simpleclouder.com/home 代替https://fit.simpleclouder.com/home

goto this URL https://fit.simpleclouder.com/#/home . 转到此URL https://fit.simpleclouder.com/#/home

well for the sake of simplicity we use hash based routing. 为了简单起见,我们使用基于哈希的路由。

In your Router config pass { useHash: true } as the second argument 在您的路由器配置中,传递{useHash:true}作为第二个参数

const rootRouting: ModuleWithProviders = RouterModule.forRoot([
  {
    path: '',
    component: HomeComponent
  },
  {
    path: 'profile',
    component: ProfileComponent
  }
], { useHash: true });

so this would make a hash based routing as its guaranteed to work in virtually any environment setup. 因此这将确保基于散列的路由几乎可以在任何环境设置中正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM