GVKun编程网logo

typescript – 如何使用angular2中的链接参数数组从子组件导航到上层路由?

3

以上就是给各位分享typescript–如何使用angular2中的链接参数数组从子组件导航到上层路由?,同时本文还将给你拓展javascript–angular2(typescript)从另一个文件

以上就是给各位分享typescript – 如何使用angular2中的链接参数数组从子组件导航到上层路由?,同时本文还将给你拓展javascript – angular2(typescript)从另一个文件导出变量、javascript – Angular2中的ES6-shim与Typescript lib.es6.d.ts冲突、javascript – 如何在angular2中的模块之间路由?、javascript – 如果它们不是Angular2中的父子组件,如何在组件之间传递数据?等相关知识,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

typescript – 如何使用angular2中的链接参数数组从子组件导航到上层路由?

typescript – 如何使用angular2中的链接参数数组从子组件导航到上层路由?

我的AppComponent有一个@RouteConfig装饰器,用于定义顶级路径:

@RouteConfig([
  {
    path: '/',name: 'Home',component: HomeComponent
  },{
    path: '/about',name: 'About',component: AboutComponent
  },{
    path: '/profile/...',name: 'Profile',component: ProfileComponent
  }
])

export class AppComponent {
}

我的ProfileComponent有一个@RouteConfig装饰器,用于定义Profile子路径:

@RouteConfig([
  {path: '/',component: ProfileDetailsComponent,name: 'View',useAsDefault: true},{path: '/:id',name: 'Public'},{path: '/edit',component: ProfileEditorComponent,name: 'Edit'},])

export class ProfileComponent {
}

当我在ProfileDetailsComponent中时,我可以重定向到其他Profile路由但不能重定向到其他路由.我想避免使用navigateByUrl指定url并使用navigate来改为使用路由名称.例如.:

this.router.navigate(['View']); // Works
this.router.navigate(['About']); // Raises error that it does not exist

我在这里读到这个答案:Angular 2 – How to navigate to another route using this.router.parent.navigate(‘/about’)

它用:

this.router.parent.navigate(['About']);

这有点好,但只有当我在申报时知道重定向应该去的时候才能解决我的问题.我有多个级别的嵌套,并在运行时确定目标路由.我正在寻找一种方法来做类似的事情:

this.router.navigate(['Level1','Level2','Level3']);

这允许我在某处跟踪目标路线的完全限定名称.这有可能吗?

解决方法

这是一个微服务派上用场的地方,请查看 service docs here.这个想法是父母可以监听孩子的导航请求,孩子可以根据需要广播所说的变化.

让我们从一个简单的对象开始,它将存储路由名称和可选参数:

export class NavArgs {
   constructor(routeName: string,args?: any) { }
}

现在让我们定义微服务,使用rxjs这很容易:

import { Injectable } from '@angular/core'
import { Subject }    from 'rxjs/Subject';

@Injectable()
export class NavigationService {
  private navigationRequestedSource = new Subject<string>();

  onNavigationRequested$= this.navigationRequestedSource.asObservable();

  requestNaivation(navArg: string) {
    this.navigationRequestedSource.next(mission)
  }
}

现在,子组件可以使用它并请求导航:

export class SomeChild {
   constructor(navService: NavigationService) { }
   invoke() {
      this.navService.requestNaivation(new NavArgs('SomeRoute'));
   }
}

并且父组件可以侦听所述请求并对其执行操作:

export class SomeParent {
   constructor(navService: NavigationService,router: Router) {
      this.navService
          .onNavigationRequested$
          .subscribe((navArgs: NavArgs) => {
              if (navArgs) {
                  if (navArgs.args) {
                      this.router.navigate([ navArgs.routeName,navArgs.args ]);
                  }
                  else {
                      this.router.navigate([ navArgs.routeName ]);
                  }
              }
      });
   }       
}

显然这是最基本的代码示例,需要更多的导入和错误处理等.但希望你能得到这个想法.

注意

确保父服务器和子服务器都使用此服务的相同实例非常重要,以便这样做 – 您必须将其设置为使用它的最高级别的提供程序.这是因为默认情况下,Angular2为DI创建单例并使用分层方法.您必须确保不要错误地将其设置为较低级别的提供程序,否则它将无法正常工作.

javascript – angular2(typescript)从另一个文件导出变量

javascript – angular2(typescript)从另一个文件导出变量

在Nodejs中,我有一个名为variables.js的页面,它看起来像这样:
exports.var1= 'a';
exports.var2= 'b';

这个文件将我在我的应用程序中使用的变量保存在一个地方.

然后在另一个页面内我使用以下方法调用此页面:

var variables= require('./variables');

现在我可以像这样使用它来访问变量sin那个页面,例如:

alert(variables.var1);

我想在angular2(typescript)中做同样的事情.我试图玩出口和进口,但我不能让它工作.我怎样才能在angular2中使用打字稿?

解决方法

variables.ts
export var var1:string = 'a';
export var var2:string = 'b';

其他-file.ts

import {var1,var2} from './variables';

alert(var1);

要么

import * as vars from './variables';

alert(vars.var1);

另见Barrel在https://angular.io/guide/glossary#barrel

javascript – Angular2中的ES6-shim与Typescript lib.es6.d.ts冲突

javascript – Angular2中的ES6-shim与Typescript lib.es6.d.ts冲突

参见英文答案 > AngularJS 2.0 compile to ES6                                    4个
我想将使用Typescript编写的Angular2应用程序编译成ES6代码规划,最近使用Babel将其编译为ES5.但是我收到了错误:

node_modules/angular2/typings/es6-shim/es6-shim.d.ts(6,14): error TS2300: Duplicate identifier ''PropertyKey''.
<...>
node_modules/typescript/lib/lib.es6.d.ts(3841,14): error TS2300: Duplicate identifier ''PropertyKey''.
<...>

我试图解决这个问题,将指令noLib添加到我的tsconfig.json中,但是其中一个角度文件(zone.d.ts)需要导入es6-shim.d.ts

/// <reference path="es6-shim/es6-shim.d.ts" />

它与主要的脚本ES6库(lib.es6.d.ts)冲突.

我怎么解决这个问题?

解决方法:

你在tsconfig.json中试过这个吗?:
    {
        “compilerOptions”:{
          “目标”:“ES6”
        }
    }

顺便问一下,如果你已经在使用打字稿,你为什么要使用babel?他们都是编译器,你只需要一个.如果你的代码用typescript编写,typescript编译器会把它编译成js,你不能重新编译它,因为它已经编译过了.只需选择目标编译器选项是否使用es5或es6语法,如果使用es6语法编译它,请确保在应用程序中添加es6垫片,以便它在尚未识别es6的浏览器上运行时.

总结

以上是小编为你收集整理的javascript – Angular2中的ES6-shim与Typescript lib.es6.d.ts冲突全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

原文地址:https://codeday.me/bug/20190706/1397111.html

javascript – 如何在angular2中的模块之间路由?

javascript – 如何在angular2中的模块之间路由?

我正在使用angular2 Final进行开发.

我创建了3个模块.

> AppModule
> ProjectModule
> DesignerModule

以前我只有AppModule,其中我已经导入了RoutingModule&它工作得很好.

import {NgModule} from '@angular/core';
import { Routes,RouterModule } from '@angular/router';
import {ProjectManagerComponent} from './project-manager/project-manager.component';
import {DesignerComponent} from './designer/designer.component';

const appRoutes: Routes = [
  {path: '',redirectTo: 'project-manager',pathMatch: 'full'},{ path: 'project-manager',component: ProjectManagerComponent },{ path: 'designer/:id',component:DesignerComponent }  
];

@NgModule({
  imports:[RouterModule.forRoot(appRoutes)],exports:[RouterModule]
})
export class AppRoutingModule { }
export const routingComponents=[ProjectManagerComponent,DesignerComponent]

但最近我为ProjectManager和amp;创建了单独的NgModules.设计师.

我将ProjectManagerComponent,DesignerComponent保存在各自模块中的声明中.

我想知道是否可以使用相同的路由配置路由到这些模块,或者我是否需要更改某些内容.

我的路由不再工作了.

任何输入?

解决方法

代替

export const routingComponents = [ProjectManagerComponent,DesignerComponent]

你可以

export const routing = RouterModule.forRoot(appRoutes);

由于app.module.ts已经从各自的模块中了解了ProjectManagerComponent和DesignerComponent.剩下的就是教它去哪里找到它们.

你可以在你的app.module.ts里面

//所有其​​他进口商品    从’./app.routes;’导入{routing}    @NgModule({      导入:[routing,/ * Other Modules * /],    })

javascript – 如果它们不是Angular2中的父子组件,如何在组件之间传递数据?

javascript – 如果它们不是Angular2中的父子组件,如何在组件之间传递数据?

我有2个组件,它们不是子组件,只是独立组件.我想将数据从Component1传递给Component2.
可能吗?我怎么能实现这个?
你能给我一个例子或者一篇文章的链接吗?
我发现了很多文章,但它们都是关于父子组件的.

解决方法:

如果组件之间没有父子关系,那么您有两个选项可以在组件之间传递数据

>共享服务
> Ngrx套件(Redux)

现在什么时候使用,如果您的应用程序大小是中等到小,请使用行为主题或重放主题进行共享服务.
请查看此link以了解有关行为主题以及如何使用共享服务在组件之间进行通信的更多信息.

使用Ngrx就像是解决这个问题的完整长期解决方案.它遵循redux模式来解决Angular中的状态管理.
点击此链接了解有关ngrx以及如何在项目中使用它们的更多信息.

更多关于ngrx

这是一个使用整个ngrx套件状态管理footballapp的实时项目

总结

以上是小编为你收集整理的javascript – 如果它们不是Angular2中的父子组件,如何在组件之间传递数据?全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

原文地址:https://codeday.me/bug/20191008/1871346.html

关于typescript – 如何使用angular2中的链接参数数组从子组件导航到上层路由?的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于javascript – angular2(typescript)从另一个文件导出变量、javascript – Angular2中的ES6-shim与Typescript lib.es6.d.ts冲突、javascript – 如何在angular2中的模块之间路由?、javascript – 如果它们不是Angular2中的父子组件,如何在组件之间传递数据?等相关内容,可以在本站寻找。

本文标签: