当前位置: 首页 > 知识库问答 >
问题:

向路由器Angular 2传递数据[重复]

华君浩
2023-03-14

我需要将数据传递到我的路由器,以便在目的地组件中使用它,我使用了< code>NavigationExtras的< code>QueryParam,但它在URL中显示参数,还有其他传递数据的方法吗?

let navigationExtras: NavigationExtras = {
            queryParams: {
                "param1": true,
            }
        };
        this.router.navigate(['/page1/view'],navigationExtras);

共有3个答案

夏侯承恩
2023-03-14

在导航到下一页之前,请使用set方法设置参数。然后导航到下一页

myFunction1(){
    this.uploadData = param;
    this.router.navigate(['/page1/view'])
}

set uploadData (param) {
        this._param = param;
}

进入下一页后,使用 get 方法检索参数

myFunction2(){
        parameter = this.uploadData;
}   

get uploadData () {
            return this._param;
}

请注意。这只是一个sudo代码。你会不

您可以从以下链接中了解有关 getters 和 setter 的更多信息

集合方法

get方法

欧阳博超
2023-03-14

嗨试试这样的东西:

1-声明如下服务:

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


@Injectable()
export class SharedService {

  // Observable sources (change activity)
    private emitChangeActivitySource = new Subject<any>();
    // Observable streams (change activity)
    changeActivityEmitted$ = this.emitChangeActivitySource.asObservable();
    // Service (change activity)
    emitChangeActivity(change: any) {
        this.emitChangeActivitySource.next(change);
    }

}

2-然后当你想获得(订阅)它时:

import {  SharedService } from '../shared/services/index';

@Component({
    selector: 'app-dashboard',
    templateUrl: './full-layout.component.html',
    styleUrls: ['./full-layout.component.scss']
})
export class FullLayoutComponent {


    constructor(private toasterService: ToasterService,
        private currentUserService: CurrentUserService,
        private menuService: MenuService,
        private router: Router,
        private auth: AuthService,
        private _sharedService: SharedService,
        private _activitiesService: ActivitiesService,
        private _router: Router) {


   _sharedService.changeActivityEmitted$.subscribe(
            activity => {
                this.data = activity;

                }
            });

            }

            }

3-要触发它的位置:

this._sharedService.emitChangeActivitySource({name:'',descr:''});
韶浩博
2023-03-14

正如别人所说的,使用一个服务。

我在这里有一个简单的例子:

https://blogs.msmvps.com/deborahk/build-a-simple-angular-service-to-share-data/

import { Injectable } from ‘@angular/core’;
@Injectable() 
export class DataService { 
  serviceData: string; 
}

或者现在在v6和更高的角度:

import { Injectable } from ‘@angular/core’;
@Injectable({
   providedIn: 'root'
}) 
export class DataService { 
  serviceData: string; 
}
 类似资料:
  • 有什么简单的方法可以实现这一点吗? 这是我导航到路线的代码 这是我的路由模块 基本上,我希望执行与CreateAlbum组件是当前组件的子组件相同的操作,在这种情况下,我将使用@Input()

  • 是否可以在Angular2中从一条路线返回数据到另一条路线?类似于使用模态搜索/选择一些数据,然后将其返回。 应用程序从/mainRoute开始。 用户单击搜索按钮 导航到/搜索 加载新路由/组件 用户选择X数据 我“关闭”当前路线(即返回/主路线) /mainRoute组件现在具有X对象 在Angular1中,我做了我自己的路由服务,它通过promise实现了这一点: 在Angular2中是否有

  • 所以我有一些路线,比如<code>/category/tech</code>和<code</categority/tech/new<code>和<code>/category/tech/old</code>等等 他们都使用 或者我必须分别定义它们,就像

  • null 或使用数组 然而,在我们的应用程序中,我看到开发人员使用的语法是 请注意,没有array[],但是两个回调函数以逗号分隔的形式传递。这起作用了。只是好奇怎么会?

  • 在几个儿童显示器中的一个中: 似乎我不能直接将数据注入。看起来我在web控制台中遇到了错误: 这有点道理,但是我该怎么做: 从服务器从父组件中获取“节点”数据? 是否将从服务器检索到的数据传递到子路由器插座? 的工作方式似乎不同。

  • 我正在研究这个angular2项目,其中我使用从一个组件导航到另一个组件。 有两个组件。i、 例如, 我想从PagesComponent导航到DesignerComponent。 到目前为止其路由正确但我需要通过 我尝试使用RouteParams,但它的获取页面对象。 下面是我的代码: 页面.组件. ts 在html中,我正在执行以下操作: 在 DesignerComponent 构造函数中,我完