How to correctly open an ItemView?

In the unofficial documentation, the best way to open a custom view is to first register the view:

    this.registerView(
      VIEW_TYPE_EXAMPLE,
      (leaf) => new ExampleView(leaf)
    );

And then activate the view:


  async activateView() {
    this.app.workspace.detachLeavesOfType(VIEW_TYPE_EXAMPLE);

    await this.app.workspace.getRightLeaf(false).setViewState({
      type: VIEW_TYPE_EXAMPLE,
      active: true,
    });

    this.app.workspace.revealLeaf(
      this.app.workspace.getLeavesOfType(VIEW_TYPE_EXAMPLE)[0]
    );
  }

but this solution doesn’t seem to fit my need( I need to pass in params ). I’m currently opening an ItemView from a prompt. And I’ve got the view appearing with my passed in parameters like such:

  await this.app.workspace.getLeaf(false).setViewState({
      type: VIEW_TYPE_EXAMPLE,
      active: true
  });

  const leaf = this.app.workspace.getLeavesOfType(VIEW_TYPE_EXAMPLE)[0]
                    
  this.app.workspace.getLeaf().open(new TestItemView(leaf, params))

Notice I don’t have to register the view at all. I’m left wondering, is this an alright way to open a view? I tried setting the view state (setViewState) while implementing the original way, but was unable to actually retrieve the passed in values within the custom view.

I don’t know that it answers your question, but the unofficial documentation has been replaced by official documentation: https://docs.obsidian.md/

1 Like

Thank you for the response, but sadly it doesn’t help. I mainly referred to the unofficial documentation due to the example they provided.

Here’s an easy way to pass the params

this.registerView(
	VIEW_TYPE_CUSTOM,
	(leaf) => new CustomPluginView(leaf, *your_param)
);

Correspondingly, in the view component, accept the params

export class CustomPluginView extends ItemView {
    param: type_of_param

	constructor(leaf: WorkspaceLeaf, param: type_of_param) {
		super(leaf);
		this.param = param;
	}
......

The calling of the view activateView() stays the same.

1 Like