【问题标题】:Pass parameter to MdDialog in Angular Material 2将参数传递给 Angular Material 2 中的 MdDialog
【发布时间】:2017-04-11 02:23:00
【问题描述】:

我正在使用 Angular Material 2,我想用 MdDialog 打开一个对话框窗口,其中显示有关存储在 firebase 中的用户的一些信息。

@Injectable()
export class TweetService {

  dialogRef: MdDialogRef<TweetDialogComponent>;

  constructor(public dialog: MdDialog) {
  }

  sendTweet(viewContainerRef: ViewContainerRef) {
    let config = new MdDialogConfig();
    config.viewContainerRef = viewContainerRef;

    this.dialogRef = this.dialog.open(TweetDialogComponent, config);

    this.dialogRef.afterClosed().subscribe(result => {
      this.dialogRef = null;
    });
  }
}

@Component({
  selector: 'app-tweet-dialog',
  templateUrl: './tweet-dialog.component.html'
})
export class TweetDialogComponent implements OnInit {
  private user: FirebaseObjectObservable<any[]>;

  constructor(
    public dialogRef: MdDialogRef<TweetDialogComponent>,
    private usersService: UsersService,
    private authService: AuthService) { }

  ngOnInit() {
    let uid = this.authService.getUser().uid;
    this.user = this.usersService.getUser(uid);
  }

}

模板就是这么简单的atm

<h1>{{ (user | async)?.email }}</h1>

用户存储在 Firebase 中,问题是对话窗口会在短时间内显示为 null,直到检索到用户。所以我想,好吧,也许在 TweetService 中检索用户并将其作为参数传递给 TweetDialogComponent 是个好主意,但后来我意识到我不知道该怎么做。

我看到了这个angular2-material-mddialog-pass-in-variable,所以我尝试了这个

@Injectable()
export class TweetService {

  private dialogRef: MdDialogRef<TweetDialogComponent>;
  private user: FirebaseObjectObservable<any[]>;

  constructor(
    private dialog: MdDialog,
    private usersService: UsersService,
    private authService: AuthService) {
  }

  getUser(): FirebaseObjectObservable<any[]> {
    return this.user;
  }

  sendTweet(viewContainerRef: ViewContainerRef) {
    let config = new MdDialogConfig();
    config.viewContainerRef = viewContainerRef;

    let uid = this.authService.getUser().uid;
    this.user = this.usersService.getUser(uid);

    this.dialogRef = this.dialog.open(TweetDialogComponent, config);

    this.dialogRef.afterClosed().subscribe(result => {
      this.dialogRef = null;
    });
  }
}

@Component({
  selector: 'app-tweet-dialog',
  templateUrl: './tweet-dialog.component.html'
})
export class TweetDialogComponent implements OnInit {
  private user: FirebaseObjectObservable<any[]>;

  constructor(
    public dialogRef: MdDialogRef<TweetDialogComponent>,
    private tweetService: TweetService) { }

  ngOnInit() {
    this.user = this.tweetService.getUser();
  }

}

但这给了我一个错误Can't resolve all parameters for TweetDialogComponent: (MdDialogRef, ?).

知道如何做到这一点吗? 谢谢,

更新

似乎这可能与桶中的导入顺序有关,但我没有使用桶,我是直接从文件中导入。 这是我的 ngModule 声明(抱歉,有点长……)

@NgModule({
  declarations: [
    AppComponent,
    ProfileComponent,
    PeopleComponent,
    TimelineComponent,
    TweetDialogComponent,
    ProfilePipe
  ],
  entryComponents: [
    TweetDialogComponent
  ],
  imports: [
    routing,
    BrowserModule,
    AuthModule,
    AngularFireModule.initializeApp(firebaseConfig, firebaseAuthConfig),
    MaterialModule.forRoot()
  ],
  providers: [
    AUTH_PROVIDERS,
    AuthGuard,
    UsersService,
    { provide: TweetService, useClass: TweetService },
    { provide: LocationStrategy, useClass: HashLocationStrategy },
    { provide: APP_BASE_HREF, useValue: '/' }
  ],
  bootstrap: [ AppComponent ]
})
export class AppModule {
}

TweetService 在我的 AppComponent 中运行良好,因此提供者应该没有问题。 这是我的 TweetDialogComponent 中的导入顺序(我看不出任何错误)。

import { Component, OnInit } from '@angular/core';
import { MdDialogRef } from '@angular/material/dialog';

import { FirebaseObjectObservable } from 'angularfire2';

import { TweetService } from '../../shared/services/tweet.service';

项目的结构(对于受影响的组件)是这样的:

src/app/
       /app.module.ts
       /app.component.ts
       /shared/services/
                       /tweet.service.ts
                       /users.service.ts
       /components/tweet-dialog/
                               /tweet-dialog.component.ts

【问题讨论】:

  • 这是一些工作,谢谢!但我无法在 plunker 中重现它……我查看了所有内容,但找不到缺陷。我猜它可能与 ngModule 有关,因为它是唯一与我的代码不同的地方。该错误是否意味着找不到 TweetService 的提供者或找不到 TweetService 的实例或什么?
  • 尝试打印console.log(TweetService); 前面的TweetDialogComponent 组件。可能您的导入顺序错误。这是一个众所周知的问题stackoverflow.com/questions/37997824/…
  • 但我没有使用桶...我无法在 TweetDialogComponent 中打印 console.log,因为错误会破坏构造函数中的执行。
  • @Component({ selector: 'app-tweet-dialog', templateUrl: './tweet-dialog.component.html' }) export class TweetDialogComponent implements OnInit {之前打印出来`你能告诉我们你的项目结构吗?

标签: angular angular-material2


【解决方案1】:

您面临循环依赖。 (TweetDialogComponent --> TweetService --> TweetDialogComponent)

您可以使用抽象类来解决问题:

base-tweet.service.ts

import { ViewContainerRef } from '@angular/core';

export abstract class BaseTweetService {
  getUser() {};

  sendTweet(viewContainerRef: ViewContainerRef) {}
}

app.module.ts

{ provide: BaseTweetService, useClass: TweetService },

app.component.ts

constructor(
    ...
    private tweetService: BaseTweetService, 

tweet-dialog.component.ts

constructor(
  ...
  private tweetService: BaseTweetService) {  

tweet.service.ts

export class TweetService implements BaseTweetService {

另见

【讨论】:

    猜你喜欢
    • 2017-02-24
    • 2015-09-23
    • 2018-01-20
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 2017-04-21
    • 2017-07-30
    • 2017-05-31
    相关资源
    最近更新 更多