[英]Using object destructuring instead of default parameters for my api calls
I'm trying to use object destructuring instead of default parameters for my api calls in my vue component.我正在尝试对 vue 组件中的 api 调用使用对象解构而不是默认参数。 Can someone tell me why this would not work?
有人能告诉我为什么这行不通吗? They are both methods in my component, the caps lock variables are set outside the component and the this ones are pulled from data.
它们都是我的组件中的方法,大写锁定变量设置在组件外部,而 this 是从数据中提取的。
buildUrl() {
const options = {
parm1: PARM_1,
parm2: PARM_2,
parm3: this.parm3,
parm4: this.parm4
};
const { parm1, parm2, parm3, parm4 } = options;
return `things?parm1=${parm1}&parm2=${parm2}&parm3=${parm3}&parm4=${parm4}`;
}
async foo() {
const { parm1, items: things } = await this.$axios.$get(
this.buildUrl({ parm1: this.parm1 + 1 })
);
this.parm1 = parm1;
this.things.push(...things);
},
It looks like you don't accept any parameters in the buildUrl
function.看起来您不接受
buildUrl
函数中的任何参数。 But in foo
you're passing an object to buildUrl
.但是在
foo
您将一个对象传递给buildUrl
。
On a side note, to handle mass url query params, it's easier to use the new and shiny URLSearchParams API if browser support allows it (that means: no IE).附带说明一下,为了处理大量 url 查询参数,如果浏览器支持允许,使用新的闪亮的URLSearchParams API 会更容易(这意味着:没有 IE)。
So change your code to:因此,将您的代码更改为:
buildUrl({
parm1 = PARM_1,
parm2 = PARM_2,
parm3 = this.parm3,
parm4 = this.parm4
}) {
const params = new URLSearchParams({ parm1, parm2, parm3, parm4 });
return `things${params}`;
}
What I think what you want is something like this:我认为你想要的是这样的:
buildUrl({
parm1 = PARM_1,
parm2 = PARM_2,
parm3 = this.parm3,
parm4 = this.parm4
}) {
return `things?parm1=${parm1}&parm2=${parm2}&parm3=${parm3}&parm4=${parm4}`;
}
async foo() {
const { parm1, items: things } = await this.$axios.$get(
this.buildUrl({ parm1: this.parm1 + 1 })
);
this.parm1 = parm1;
this.things.push(...things);
},
Which means parm1
defaults to PARM_1
, parm2
defaults to PARM_2
, parm3
defaults to this.parm3
and parm4
defaults to this.parm4
.这意味着
parm1
默认为PARM_1
, parm2
默认为PARM_2
, parm3
默认为this.parm3
, parm4
默认为this.parm4
。
You're not accepting any parameters at all in buildUrl
.您在
buildUrl
根本不接受任何参数。 It looks like you want to accept a single parameter that you destructure and provide various defaults for.看起来您想要接受一个您解构的参数并为其提供各种默认值。 That looks like this:
看起来像这样:
buildUrl({parm1 = PARM_1, parm2 = PARM_2, parm3: this.parm3, parm4: this.parm4} = {}) {
return `things?parm1=${parm1}&parm2=${parm2}&parm3=${parm3}&parm4=${parm4}`;
}
Or with more line breaks:或者有更多的换行符:
buildUrl({
parm1 = PARM_1,
parm2 = PARM_2,
parm3: this.parm3,
parm4: this.parm4
} = {}) {
return `things?parm1=${parm1}&parm2=${parm2}&parm3=${parm3}&parm4=${parm4}`;
}
This part is the destructuring:这部分是解构:
{parm1 = PARM_1, parm2 = PARM_2, parm3: this.parm3, parm4: this.parm4}
...which handles providing defaults for any properties not supplied by the caller. ...它处理为调用者未提供的任何属性提供默认值。
This part makes the entire parameter optional by providing a default value for it:这部分通过为其提供默认值使整个参数成为可选:
= {}
That makes buildUrl()
work, using all defaults.这使得
buildUrl()
工作,使用所有默认值。 (The overall anonymous parameter is defaulted to {}
, then all of the destructured parameters get their defaults because {}
doesn't have properties for them.) Without this overall default, you'd need to use buildUrl({})
instead. (整体匿名参数默认为
{}
,然后所有解构参数都获得它们的默认值,因为{}
没有它们的属性。)如果没有这个整体默认值,您需要改用buildUrl({})
。
Side note: Query parameters (both name and value) must be URI-encoded.旁注:查询参数(名称和值)必须是 URI 编码的。 Your code isn't doing that.
你的代码没有这样做。 The names of your parameters don't have any characters that need encoding, so you can skip those (but you could do them in case you change them later), but the values presumably vary and need encoding.
您的参数名称没有任何需要编码的字符,因此您可以跳过这些字符(但您可以跳过它们,以防以后更改它们),但值可能会有所不同并且需要编码。
buildUrl({
parm1 = PARM_1,
parm2 = PARM_2,
parm3: this.parm3,
parm4: this.parm4
} = {}) {
// Ensures order (on up-to-date JavaScript engines) and gives us an object to use
const params = {parm1, parm2, parm3, parm4};
return "things?" + Object.entries(params).map(([key, value]) => `encodeURIComponent(key)=encodeURIComponent(value)`).join("&");
}
(Yes, order is really guaranteed there provided none of the parameter names is all digits. This was partially guaranteed by ES2015, and now ES2020 extends that to Object.entries
and others because that's what all major JavaScript engines do anyway.) (是的,如果没有参数名称全是数字,确实可以保证顺序。ES2015 部分保证了这一点,现在 ES2020 将其扩展到
Object.entries
和其他,因为无论如何所有主要的 JavaScript 引擎都是这样做的。)
Or of course:或者当然:
buildUrl({
parm1 = PARM_1,
parm2 = PARM_2,
parm3: this.parm3,
parm4: this.parm4
} = {}) {
return "things" +
`?parm1=${encodeURIComponent(parm1)}` +
`&parm2=${encodeURIComponent(parm2)}` +
`&parm3=${encodeURIComponent(parm3)}` +
`&parm4=${encodeURIComponent(parm4)}`;
}
Or use URLSearchParams as nirazul shows you .或者使用URLSearchParams作为nirazul 向您展示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.