English 中文(简体)
为什么我无法在一系列物体中获取财产,如果不是在 lo中?
原标题:Why I cannot access a property in an array of objects if not in a loop?

I m working with Vue 3. If I write this:

<p v-for="(prs, index) in settings.pressure">{{settings.pressure[0].value}}</p>

I see the value correctly, while if I use this (the one I need):

<p>{{settings.pressure[0].value}}</p>

它犯了一个错误:

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 0 )

如何在第一版中加以界定,第二版中未作界定?

问题回答

似乎这种环境。 当模板装载时,压力就没有界定。

You are able to for in loop an undefined element without throwing an error. Since it does not loop over anything, <p>{dings.pressure?>[0]. will not be evaluated. As such it gives time for your settings.pressure state to be loaded which will render the element as expected.

下面的法典将印刷“未发现错误”

try {
    for (let i in undefined) {}
    console.log("No error thrown")
} catch (error) {
    console.log("Error thrown")
}

然而,你的第二个答案是,立即进入阵列中未界定的第一个要素。 这将使你留下错误。

您不妨尝试使用一种选择,以便在国家装满之前不会出现错误。

<p>{dings.pressure?>[0].

<>t>v-for Directive(无论该代码如何)的模板代码在收集的<>>>>>>>>每一项目上均已执行<><>><>>>>>>>><>t/sup>/em>> > >。

So when settings.pressure is an empty array ([]) it runs the template code once for each item in the array: 0 times!

But when you run settings.pressure[0].value outside of v-for and settings.pressure is empty, the code tries to find the first item in settings.pressure (which is undefined). And then it tries to access the .value of undefined, like it was an object. Whenever you try to access any property of null or undefined, JavaScript will throw an error telling you they don t have any properties (because they can t have them, by design) and therefore they can t be queried for properties.


<<><>><>>><>>>>>>>>>>>>>>---------------->-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.