简体   繁体   English

如何理解,然后有效地使用 vue 中的控制台警告和错误?

[英]How to make sense of, and then effectively use the console warnings and errors in vue?

I (an amateur home developer) am using Vue for about two or three years (recently with the quasar framework) and one of my main problems is how to make sense, and then actually use the warnings or errors I get in the console.我(一个业余家庭开发人员)使用 Vue 大约两三年(最近使用 quasar 框架),我的主要问题之一是如何理解,然后实际使用我在控制台中收到的警告或错误。 Here is an example from a moment ago:这是刚才的一个例子:

在此处输入图像描述

I finally found the row that was causing the problem, but I had to track it in the code and remember what I recently changed to pinpoint the error.我终于找到了导致问题的row ,但我必须在代码中跟踪它并记住我最近更改的内容以查明错误。 This is just an example to illustrate the general problem.这只是说明一般问题的一个例子。

In Python, when I see an error, I also see which exact line triggered it.在 Python 中,当我看到错误时,我还看到了哪一行触发了它。 In my IDE it is enough to click on it and I am there.在我的 IDE 中,点击它就足够了,我就在那里。

I am sure that the console information above also make sense for Vue but at no place I can see the actual line causing the problem.我确信上面的控制台信息对 Vue 也有意义,但我在任何地方都看不到导致问题的实际行。

How should I read this console log to find the erroneous line in my Vue code?我应该如何阅读这个控制台日志来找到我的 Vue 代码中的错误行?

Property or method "props" is not defined未定义属性或方法“props”

 [Vue warn]: Property or method "props" is not defined on the instance but referenced during render. [...] found in ---> <MainLayout> <App> at src/App.vue <Root>

This indicates <MainLayout> 's template contains a reference to " props ", which is not declared in the component's data or props (or in setup() if using the Composition API).这表明<MainLayout>template包含对“ props ”的引用,该引用未在组件的dataprops中声明(如果使用组合 API,则在setup()中声明)。 For example:例如:

<template>
  <div>{{ props }}</div>
</template>

<script>
export default {
  props: {
    foo: String,
    // "props" not declared here
  },
  data() {
    return {
      bar: 1,
      // "props" not declared here
    }
  },
  setup() {
    return {
      // "props" not declared here
    }
  }
}
</script>

Cannot read property 'row' of undefined无法读取未定义的属性“行”

 [Vue warn]: Error in render. "TypeError: Cannot read property 'row' of undefined" found in ---> <MainLayout> <App> at src/App.vue <Root>

This indicates <MainLayout> 's template contains a sub-property reference to an object that is undefined (eg, myUndefinedObj.row ).这表明<MainLayout>template包含对undefined的 object 的子属性引用(例如, myUndefinedObj.row )。 Given the first warning above, that object is probably props :鉴于上面的第一个警告, object 可能是props

<template>
  <div>{{ props.row }}</div>
          ^^^^^^
</template>

I'm guessing you actually have a row prop declared in props , and you're trying to reference it in the template.我猜你实际上在 props 中声明了一个row props ,并且你试图在模板中引用它。 You can access props or data directly in the template without any prefixes:您可以直接在模板中访问propsdata ,无需任何前缀:

<template>
  <div>{{ row }}</div>
</template>

<script>
export default {
  props: {
    row: Number
  }
}
</script>

Error location错误位置

TypeError: Cannot read property 'row' of undefined at Proxy.render (MainLayout.vue?1074:54) [...]

The stacktrace in the last warning indicates the filename and location of the error in the form of FILENAME?LINE:COLUMN :最后一个警告中的堆栈跟踪以FILENAME?LINE:COLUMN的形式指示错误的文件名和位置:

MainLayout.vue?1074:54
   ^            ^    ^
filename        |    |
               line  |
                    column

Many modern IDEs show line number and column in the status bar at the bottom.许多现代 IDE 在底部的状态栏中显示行号和列。 Visual Studio Code shows them in the bottom right corner: Visual Studio Code 在右下角显示它们:

VS Code 状态栏

Clicking on that line indicator box opens a prompt to jump to a specific line number and column in : -delimited form, so you could copy-paste the line/column from the error ( 1074:54 ) into the prompt to jump to the exact location indicated.单击该行指示器框会打开提示以跳转到特定的行号和列:分隔形式,因此您可以将错误( 1074:54 )中的行/列复制粘贴到提示中以跳转到确切的指示的位置。

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

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