yanyue404 / blog

Just blog and not just blog.
https://yanyue404.github.io/blog/
Other
88 stars 13 forks source link

Fix Vue warning #199

Open yanyue404 opened 3 years ago

yanyue404 commented 3 years ago

常见 warning

1. 组件注册不正确

[Vue-warn]:Unknown custom element: - did you register the component correctiy?`

2. 子组件试图直接更改父组件

[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "value"`

注意在 JavaScript 中对象和数组是通过引用传入的,所以对于一个数组或对象类型的 prop 来说,在子组件中改变变更这个对象或数组本身将会影响到父组件的状态。

这里有两种常见的试图变更一个 prop 的情形:

props: ['initialCounter'],
data: function () {
  return {
    counter: this.initialCounter
  }
}
props: ['size'],
computed: {
  normalizedSize: function () {
    return this.size.trim().toLowerCase()
  }
}
// father
<CusPopup ref="myPopup" v-model="activeIndex"></CusPopup>

// child
export default {
  components: {
    Popup
  },
  model: {
    prop: 'index',
    event: 'change'
  },
  props: {
    index: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
       activeIndex: this.index
    }
  },
  watch: {
    index(newVal) {
      this.activeIndex = newVal
    },
  }
}

3. 引用一个不存在的变量

[Vue warn]: Property or method "name" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties. (found in )

4. 取多级数据值

例如: movie_data.subject_collection.name

[Vue warn]: Error in render function: “TypeError: Cannot read property ‘name’ of undefined”

<h2 v-text="module_title || movie_data.subject_collection.name"></h2>

// 判断上一级数据是否存在即可
<h2 v-text="module_title || (movie_data.subject_collection && movie_data.subject_collection.name)"></h2>

5.无法在'Node'上执行'insertBefore':要在其之前插入新节点的节点不是该节点的子节点

[Vue warn]: Error in nextTick: "NotFoundError: Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node."

<!-- Throw Error  -->
<el-table-column fixed="right" label="操作" align="center" width="100">
  <template scope="scope">
    <el-button
      v-show="scope.row.status === '1'"
      v-permission="['/prtOrder/Abolition']"
      size="small"
      type="warning"
    >
      废除
    </el-button>
  </template>
</el-table-column>

Fix: v-show="scope.row.status === '1' && checkPermission(['/prtOrder/Abolition'])"

处理异常

参考