Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.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 2中重定向页面_Angular_Routes_Angular Ui Router_Angular2 Routing_Router - Fatal编程技术网

如何在angular 2中重定向页面

如何在angular 2中重定向页面,angular,routes,angular-ui-router,angular2-routing,router,Angular,Routes,Angular Ui Router,Angular2 Routing,Router,当前页面为 我想去 如何做到这一点在角 我有 this.router.navigate(['/single'], {queryParams: {month: this.data.month}}); 但它不起作用。而且有错误 Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: '/single' 我们可以使用路由模块来实现这一点。在本例中,我们将使用两个组件:Component1和Componen

当前页面为

我想去

如何做到这一点在角

我有

this.router.navigate(['/single'], {queryParams: {month: this.data.month}});
但它不起作用。而且有错误

Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: '/single'

我们可以使用路由模块来实现这一点。在本例中,我们将使用两个组件:Component1Component2创建一个名为app.routing.ts的文件:

import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { Component1 } from './component1.component';
import { Component2 } from './component2.component';
const appRoutes: Routes = 
       [{ path: 'component1', component: Component1 },
        { path: 'component2', component: Component2 }];
 export const appRoutingProviders: any[] = [];
 export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);
我已经定义了路径/component1/component2,这将允许我重定向到这两个组件

接下来,我们必须导入app.module.ts上的路由配置,如下所示:

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent }   from './app.component';
import { routing, appRoutingProviders }  from './app.routing';
import { Component1 } from './component1.component';
import { Component2 } from './component2.component';

@NgModule({
imports:      [ BrowserModule, routing ],
declarations: [ AppComponent, Component1, Component2],
providers:    [appRoutingProviders],
bootstrap:    [ AppComponent ]
})
export class AppModule { }
现在,您可以通过将路径传递到常规链接的routerLink属性来重定向:

<a routerLink="/component1" >Component 1</a>
<a routerLink="/component2" >Component 2</a>
组件1
构成部分2
我尝试过这个例子,希望这能有所帮助……

这段代码将帮助您解决重定向问题,我还附上了JSFIDLE[链接文本](http://jsfiddle.net/5DMjt/25081/)
    This code will help you to solve redirection problem of yours i am also attaching jsfiddle[link text](http://jsfiddle.net/5DMjt/25081/)


            <html ng-app="myApp">
            <head>
            </head>
            <body ng-controller="myCtrl">
                <p> <a href="https://docs.woocommerce.com/document/create-a-plugin/"><button>Click me to redirect from template</button></a></p>
                <p ng-click="myFunction()"> <button>Click me to redirect from controller</button></p>
            <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
            <script>
                var app = angular.module('myApp',[]);
                app.controller('myCtrl',function($window,$scope){
                    $scope.myFunction = function(){
                    $window.location.href = 'http://delmarts.com/'; //You should have http here.
                    }
                });
            </script>
            </body>
            </html>

单击我从控制器重定向

var-app=angular.module('myApp',[]); app.controller('myCtrl',函数($window,$scope){ $scope.myFunction=function(){ $window.location.href='1http://delmarts.com/“;//这里应该有http。 } });
可能重复的可能重复的可能重复的您是否为项目配置了路由?您是否在项目中为“single”设置了路由?