Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.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 使用角度';s位置类是否正确_Angular - Fatal编程技术网

Angular 使用角度';s位置类是否正确

Angular 使用角度';s位置类是否正确,angular,Angular,我在http://myexaple.com/subFolder1 我想要this.location.go将顶部的URL从http://myexaple.com/subFolder1至http://myexaple.com/subFolder2 就这么简单 所以,我走了 this.location.go('/subFolder2');//你还会做什么 但结果是 医生们觉得我所做的似乎是正确的 我错过了什么 是的,要从一条路由重定向到另一条路由,您不应该使用location.go,它通常用于在浏览

我在
http://myexaple.com/subFolder1

我想要
this.location.go
将顶部的URL从
http://myexaple.com/subFolder1
http://myexaple.com/subFolder2

就这么简单

所以,我走了

this.location.go('/subFolder2');//你还会做什么

但结果是

医生们觉得我所做的似乎是正确的


我错过了什么

是的,要从一条路由重定向到另一条路由,您不应该使用
location.go
,它通常用于在浏览器上获取
标准化url

相反,您应该使用
Router
API的
navigate
方法,在使用
[routerLink]
指令创建
href
时,该方法将采用
['routeName']

如果您只想通过URL重定向,那么可以使用
navigateByUrl
,它将URL作为字符串,类似于
router.navigateByUrl(URL)


这是因为
location
将所有内容规范化为当前路径。试着改用
路由器
:但路由器对我来说是一种过度使用。必须有另一种方法对URL进行更改(不需要像router那样真正开始新的导航过程)。在文档中,它谈到了“prepareExternalUrl”,这可能是一种解决方法。我不确定。但我无法尝试。如何在代码中使用prepareExternalUrl?您是否执行
此.location.go.prepareExternalUrl('/subFolder2')
?经过一点挖掘,我发现了以下内容:。基本上,您可以使用javascript,如下所示:
window.history.pushState('data','title',“/subFolder2”)“但路由器对我来说太过分了”。。很难相信,嗯,是的,断章取义,听起来很奇怪,但嘿,这就是这个特定应用程序和那个特定实例中的情况。谢谢你,但在我的使用案例中,我不能导航到那个URL。我只更新浏览器地址栏上的URL(以便可以复制/共享等),但我不想导航到该URL。(因为这样做会改变其他事情,我不需要进入其中。)“导航”只是为了更新浏览器地址栏吗?它不会触发真正导航到该URL的效果,对吗?如果是这样,这可能就是解决方案。对不起,您使用的是什么版本的
Angular
?它看起来像是Alpha或Beta。我使用的是angular 4.3 developer033。
import { 
  ROUTER_DIRECTIVES, 
  RouteConfig, 
  ROUTER_PROVIDERS, 
  Location, //note: in newer angular2 versions Location has been moved from router to common package
  Router 
} from 'angular2/router';

constructor(location: Location, 
            public _userdetails: userdetails,
            public _router: Router){
    this.location = location;
}
login(){
    if (this.username && this.password){
        this._userdetails.username = this.username;
        //I assumed your `/home` route name is `Home`
        this._router.navigate(['Home']); //this will navigate to Home state.
        //below way is to navigate by URL
        //this.router.navigateByUrl('/home')
    }
    else{
        console.log('Cannot be blank');
    }
}