vue3报错 Unexpected mutation of “xxx“ prop.(eslintvue/no-mutating-props)

这篇具有很好参考价值的文章主要介绍了vue3报错 Unexpected mutation of “xxx“ prop.(eslintvue/no-mutating-props)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

vue3报错 Unexpected mutation of “xxx“ prop.(eslintvue/no-mutating-props)

unexpected mutation of

eslint校验报这个错,其实是Vue的单向数据流的概念,因为识别到子组件中修改了props值

我这里踩到这个坑是这么操作的,我在父组件中给子组件传了个值,然后再子组件中v-modle这个值,于是就给我报了这个错!

复现场景如下:
父组件中

<enter-school ref="enterSchoolRef" :student-info="selectRows" />

子组件中

<template>
    <el-form ref="formRef" class="enterForm" inline :model="form" :rules="rules" @submit.prevent>
      <el-input
          v-model="studentInfo[0].name"
          clearable
          placeholder="请输入姓名"
        />
     </el-form>
</template>
<script>
    props: {
      studentInfo: {
        type: Array, //类型
        default: null //默认值
      },
    },
</script>

报错就在v-model="studentInfo[0].name"
不应该在子组件中直接双向绑定父组件传递过来的值
解决方案:

  1. 计算属性 依赖该props进行计算/转换
<template>
 <el-input
      v-model="studentName"
      clearable
      placeholder="请输入姓名"
 />
</template>
<script>
    import { computed } from 'vue'
    props: {
      studentInfo: {
        type: Array, //类型
        default: null //默认值
      },
    }
    setup(props) {
      const studentName = computed(() =>
        props.studentInfo &&
        props.studentInfo.length > 0 ? props.studentInfo[0].name : null
      )
      return {
          studentName 
     }
    }
</scirpt>
  1. 定义中间变量 在子组件内的定义一个变量,并将接收的props当作其初始值:
<template>
 <el-input
      v-model="studentName"
      clearable
      placeholder="请输入姓名"
 />
</template>
<script>
    import { computed, defineComponent, reactive, toRefs } from 'vue'
    props: {
      studentInfo: {
        type: Array, //类型
        default: null //默认值
      },
     }
    export default defineComponent({
    setup(props) {
      const state = reactive({
        studentName : props.studentInfo[0].name
      })
      return {
          ...toRefs(state),
     }
    }
   })
</scirpt>

我就是用的方案1,搞个计算属性解决了文章来源地址https://www.toymoban.com/news/detail-600308.html

到了这里,关于vue3报错 Unexpected mutation of “xxx“ prop.(eslintvue/no-mutating-props)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent..

     错误:[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\\\". 译文:[Vue警告]:避免直接改变prop,因为每当父组件重新呈现时,该值将被覆盖。相反,应该使用基于

    2024年02月12日
    浏览(37)
  • vue3+ts报错:无法找到模块“xxx.vue“的声明文件,xxx隐式拥有“any“类型

    1、报错原因:typescript不能识别.vue文件 2、解决方法: 可以在vite-env.d.ts中添加以下代码,如果没有 vite-env.d.ts ,可以自己新建一个 xxx.d.ts 类型的文件即可 创建好项目之后一直报错,搜索解决掉了,自己整理所用,原文链接:vue3+ts报错解决:无法找到模块“xxx.vue”的声明文

    2024年01月21日
    浏览(31)
  • [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent c

    报错翻译:避免直接更改一个prop,因为每当父组件重新渲染时,该值都会被覆盖。相反,应使用基于prop值的数据或计算属性。正在更改的prop:“activeId” 解决办法,使用基于prop的值,即把名为activeId的prop的值放在另一个变量中,对那个变量进行修改,不修改activeId。 1、实

    2024年02月03日
    浏览(43)
  • Vue2向Vue3过度Vuex核心概念mutations

    1.定义mutations 2.格式说明 mutations是一个对象,对象中存放修改state的方法 3.组件中提交 mutations 4.练习 1.在mutations中定义个点击按钮进行 +5 的方法 2.在mutations中定义个点击按钮进行 改变title 的方法 3.在组件中调用mutations修改state中的值 5.总结 通过mutations修改state的步骤 1.定义

    2024年02月11日
    浏览(30)
  • Vue3报错:Extraneous non-props attributes (style) were passed to component but could not be automatical

    Vue3报错:Extraneous non-props attributes (style) were passed to component but could not be automatically inherited because component renders fragment or text root nodes. 翻译是:无关的非道具属性(样式)被传递给组件,但由于组件呈现片段或文本根节点而无法自动继承。 出现这个 错误的原因 是在组件的节点

    2024年02月17日
    浏览(27)
  • 解决Vue3的ts报错:类型“{}”上不存在属性“xxx”,两种方法彻底根治

    刚创建的一个Vue3和Ts的项目,结果使用Vscode打开后,修改了index.vue文件就报错了: 网上找了各种原因,有让添加jsconfig.json文件的,有让新建一个项目的,有的直接放弃ts的,哈哈哈,真的是千奇百怪都有,好吧,我这里就也出两种方法: 在tsconfig.json文件中添加一行代码:就是

    2024年02月11日
    浏览(76)
  • [Vue warn]: Error in render: “TypeError: Cannot read property ‘ xxx ‘ of undefined“报错原因和解决

    遇到问题: 能够正常渲染出界面,但控制台仍然报出“Error in render: \\\"TypeError: Cannot read property ‘0’ of undefined”错误 原因: vuex中state管理加载的数据,异步调用显示,然后vue渲染机制 异步数据先显示初始数据,再显示带数据的数据,所以上来加载时候还是一个空对象,当渲

    2024年02月11日
    浏览(35)
  • 当vue3 报错 Cannot read properties of null (reading ‘style‘)

    当你在编写代码时 发现页面不及时刷新了 浏览器控制台报下面的错误 时刚看到的时候会一很懵 那么原因是什么呢 原因是:尽管Vue 3允许一个组件模板中存在多个元素,但是如果你这样写,有时会出现上述错误。 解决方法:在模板内你写的多个标签外面包裹一层元素,或者

    2024年02月12日
    浏览(40)
  • Vue3 报错:WebSocket connection to ‘ws://X.XXX.X.XX:8080/ws‘ failed:

    问题:页面没有问题,但是打开控制台就出现如下图所示的一连串的报错信息 问题解决:   修改完后重新运行一下即可

    2024年02月13日
    浏览(75)
  • 解决报错SyntaxError:Unexpected end of JSON input

    跳转页面传递参数 报错提示 SyntaxError:Unexpected end of JSON input 原因:若对象的参数或数组的元素中遇到地址中包括? - _ . ! ~ * \\\' ( )等特殊符号时,对象/数组先要通过JSON.stringify转化为字符串再通过 encodeURIComponent 编码,接收时,先通过 decodeURIComponent 解码再通过JSON.parse转换为

    2024年02月11日
    浏览(46)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包