简体   繁体   English

Angular / Typescript 使用方法中的参数值作为 js 对象值

[英]Angular / Typescript using the parameter value from method as a js object value

I am using Ionic / Angular / Typescript and I want to dynamically state the value of a location by the value passed from the method in the parameter.我正在使用 Ionic/Angular/Typescript,我想通过从参数中的方法传递的值来动态声明位置的值。

Here is the code:这是代码:

async fileWrite(location) {
    try {
      const result = await Filesystem.writeFile({
        path: `test.txt`,
        data: `This is a test`,
        directory: location,
        encoding: FilesystemEncoding.UTF8
      });
      alert('Wrote file' + result);
    } catch (e) {
      alert('Unable to write file' + e);
    }
  }

As you can see:如你看到的:

directory: location,目录:位置,

location contains a value so it's a variable. location 包含一个值,因此它是一个变量。

I'm passing it like this:我是这样传递的:

<ion-button (click)="fileWrite('FilesystemDirectory.Cache')">FileWrite to Cache</ion-button>

For some reason it's not passing as a variable.出于某种原因,它没有作为变量传递。

How can I fix this?我怎样才能解决这个问题?

Try doing this.尝试这样做。

In your typescript class, right before constructor.在您的打字稿类中,就在构造函数之前。

directory = FilesystemDirectory;

and then inside you template.然后在你的模板里面。

<ion-button (click)="fileWrite(directory.Cache)">FileWrite to Cache</ion-button>

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM