Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.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
角度路由,是重新加载整个html页面,还是只是页面的一部分?_Html_Angular - Fatal编程技术网

角度路由,是重新加载整个html页面,还是只是页面的一部分?

角度路由,是重新加载整个html页面,还是只是页面的一部分?,html,angular,Html,Angular,我想找到以下问题的答案:在角度路由中,当单击链接并生成路由url时,是重新加载整个html页面,还是只是页面的一部分 使用Angular basic教程,我在每个组件的html模板中添加了一条语句,用不同的颜色绘制一个框(代码来自和此处)。例如 <h1>{{title}}</h1> <nav> <a routerLink="/dashboard">Dashboard</a> <a routerLink="/heroes"&

我想找到以下问题的答案:在角度路由中,当单击链接并生成路由url时,是重新加载整个html页面,还是只是页面的一部分

使用Angular basic教程,我在每个组件的html模板中添加了一条语句,用不同的颜色绘制一个框(代码来自和此处)。例如

<h1>{{title}}</h1>
<nav>
  <a routerLink="/dashboard">Dashboard</a>
  <a routerLink="/heroes">Heroes</a>
</nav>
<div style="width:100px;height:100px;border:1px solid rgb(255, 255, 0);">This is app component!</div>

<router-outlet></router-outlet>
<app-messages></app-messages> 
但是在hero-detail.component.html中

<div><script>random_rgba();</script></div>
random_rgba();

重新加载页面时不会绘制彩色矩形:(

您正在使用的“routerLink”属性是Angular Router的一部分。Angular Router负责更改URL并相应地显示组件。当您使用它时,它不会刷新整个浏览器

只有在路由阵列中具有匹配项的组件才会重新加载。路由器出口上方/外部的所有组件都保持其本地状态,不会由路由重新生成

您还可以有多个路由器出口,如果是这样,则仅重新加载相关路由器出口内的部分


如果您是从更改检测的角度来看它,那么更改检测可能会在您的整个应用程序中运行,因为它会在发生的每一个其他事件中运行。不过,通过在组件内部使用更改检测策略OnPush,这是可以避免的。

您是否检查了浏览器的开发人员工具的网络选项卡@DiogoSantana谢谢你的提示。我刚才看了。没有活动,但这是因为本教程不进行任何网络调用。不清楚
const routes:routes=[{path:'',重定向到:'/dashboard',pathMatch:'full'},{path:'dashboard',component:DashboardComponent},{path:'detail/:id',component:HeroDetailComponent},{path:'heromes',component:HeroesComponent}]
如果用户点击一个id,那么这个
路由中的所有组件都重新加载吗?或者只有
HeroDetailComponent
@likejudo只有HeroDetailComponent将重新加载。我将尝试重新表述我的答案。我正在尝试测试您编写的内容并更新我的问题,但是我的JS中有一些错误,因为没有绘制彩色矩形:(我不认为您可以在Angular中在html模板中放置标记。如果您想在每次创建组件时运行一些js代码,可以将其放置在OnInit生命周期挂钩中。例如,您可以在组件类中添加以下方法:ngOnInit():void{console.log('component was created');}但创建随机彩色形状的目的是区分矩形是否改变颜色(重新加载整个页面)或组件中矩形的颜色是否改变(然后重新加载唯一组件)。
    "scripts": [
      "src/custom.js"
    ]
<div><script>random_rgba();</script></div>