整理 Vue2 项目中各种报错警告-信息

news/2024/7/9 23:35:27 标签: 前端, Vue

目录

Vue%20warn%5D%3A%20Error%20in%20v-on%20handler%20(Promise%2Fasync)%3A%20%22Error%3A%20Request%20failed%20with%20status%20code%20404%22-toc" style="margin-left:40px;">( 1 ) [Vue warn]: Error in v-on handler (Promise/async): "Error: Request failed with status code 404"

Vue%20warn%5D%3A%20Error%20in%20v-on%20handler%3A%20%22TypeError%3A%20this.todos.push%20is%20not%20a%20function%22-toc" style="margin-left:40px;">( 2 ) [Vue warn]: Error in v-on handler: "TypeError: this.todos.push is not a function"

Vue%20warn%5D%3A%20Computed%20property%20%22IsDisabled%22%20was%20assigned%20to%20but%20it%20has%20no%20setter.-toc" style="margin-left:40px;">( 3 ) [Vue warn]: Computed property "IsDisabled" was assigned to but it has no setter.

Vue%20warn%5D%3A%20Unknown%20custom%20element%3A%20%3CNavBar%3E%20-%20did%20you%20register%20the%20component%20correctly%3F%20For%20recursive%20components%2C%20make%20sure%20to%20provide%20the%20%22name%22%20option.-toc" style="margin-left:40px;">( 4 ) [Vue warn]: Unknown custom element: - did you register the component correctly? For recursive components, make sure to provide the "name" option.

Vue%20Router%204.%20Use%20the%20v-slot%20API%20to%20remove%20this%20warning%20%3A-toc" style="margin-left:40px;">一、[vue-router] 's tag prop is deprecated and has been removed in Vue Router 4. Use the v-slot API to remove this warning :

二、[Violation] Addad non-passive event listener to a scroll-blocking 'mousewheel' event. Consider marking event handler as 'passive' to make the page more responsive.

三、路由跳转错误:Uncaught (in promise) Error: Redirected when going from "/..." to "/..." via a navigation guard.

四、[WDS] Disconnected!


自己平常 Vue2 项目 中出现的一些 Bug 和 警告⚠️ , 日常记录一下 : 

Vue%20warn%5D%3A%20Error%20in%20v-on%20handler%20(Promise%2Fasync)%3A%20%22Error%3A%20Request%20failed%20with%20status%20code%20404%22">( 1 ) [Vue warn]: Error in v-on handler (Promise/async): "Error: Request failed with status code 404"

GET 发送 请求 报错 :

[Vue warn]: Error in v-on handler (Promise/async): "Error: Request failed with status code 404"

vue.config.js

// 此文件是 Vue 暴露给开发者来对 webpack 进行增量配置
// 此文件是给 NodeJs 运行 , 所以它使用 commonjs 规范
module.exports = {
  // 对于当前 Vue 项目服务器添加一个反向代理服务
  devServer: {
    // 配置代理  代理配置可以设置 n 多个
    proxy: {
      // 以什么样的请求 url 开始我才代理 此 url 一定是相对路径
      // 如果有 http / https ,则代理不生效
      '/api': {
        // 代理到的服务器是谁
        target: 'https://api.iynn.cn/film',
        // 修改 host 请求的域名为目标域名
        changeOrigin: true,
        // 请求 url 和目标 url 有一个对应关系
        // 请求 /api/login => 目标 /v1/api/login
        pathRewrite: { // 过滤 / 重写
          // '^xhl': ''
        }
      },

      'ajax': {
        target: 'https://m.maoyan.com',
        changeOrigin: true,
      }
    }
  }
}

将代理请求地址信息填写正确之后 : 重启一下 Vue 项目

数据请求成功 :


Vue%20warn%5D%3A%20Error%20in%20v-on%20handler%3A%20%22TypeError%3A%20this.todos.push%20is%20not%20a%20function%22">( 2 ) [Vue warn]: Error in v-on handler: "TypeError: this.todos.push is not a function"

调用 Vue 中提供的数组变更方法 ( push ) 完成对于数组数据的修改 时 发生 报错 :

因为 绑定了 v-model 指令的缘故 , 将 todos 数组类型转换成了字符串类型 , 所以 todos 就已经不能再使用 Vue 提供的 数组 变更方法了 , 就会一直报错 : this.todos 不是一个函数  ( 字符串类型不能使用 数组 的 方法 )

 

解决 : 将 input 输入框 上面绑定 的 v-model 指令 删除掉就 OK 了 ,

这样就不会影响到我们的 todos 的 数据类型了 

<!-- 建立一个输入框 -->
<input type="text" placeholder="请输入内容" @keyup.enter='onEnter'>

Vue%20warn%5D%3A%20Computed%20property%20%22IsDisabled%22%20was%20assigned%20to%20but%20it%20has%20no%20setter.">( 3 ) [Vue warn]: Computed property "IsDisabled" was assigned to but it has no setter.

[Vue warn]:计算属性 “IsDisabled” 已分配给,但它没有 setter。

项目中在 computed 计算属性中定义了一个 IsDisabled ,

但是它的状态是会发生改变的 , 但却只写了 函数 ( 相当于简写的 get )

所以这里控制台提示了报错信息

<el-button plain size="small" type="primary" :disabled="IsDisabled"
      >机构</el-button
    >
computed: {
    // 每个计算属性都包括一个getter(get)和一个setter(set)
    // 在计算属性内填写的函数相当于简写的get
    /* 因为 IsDisabled 值会改变, 所以不能只用下面的写法 */
    // IsDisabled() {
    //   // 计算属性函数中的return不能少
    //   return this.checkList.indexOf("isunlimit" !== -1);
    // },
    IsDisabled: {
      get: function () {
        console.log("调用了get方法");
        return this.checkList.indexOf("isunlimit" !== -1);
      },
      // 只有当 IsDisabled 中的值改变的时候才能触发set,传值的value是 IsDisabled 改变的值
      set: function (value) {
        console.log("调用了的set方法", value);
      },
    },
  },

Vue%20warn%5D%3A%20Unknown%20custom%20element%3A%20%3CNavBar%3E%20-%20did%20you%20register%20the%20component%20correctly%3F%20For%20recursive%20components%2C%20make%20sure%20to%20provide%20the%20%22name%22%20option.">( 4 ) [Vue warn]: Unknown custom element: <NavBar> - did you register the component correctly? For recursive components, make sure to provide the "name" option.

组件注册不规范导致的报错问题 :

[Vue warn]:未知自定义元素:<NavBar>-是否正确注册了组件?对于递归组件,请确保提供“name”选项。

乍眼一看 , 我的注册步骤是挑不出任何问题滴 。。

但是往往没有问题还出现报错了 , 就很有可能是你的疏忽大意 ,

尤其是 单词写错 !

百度半天 , 试来试去 到都最后也没有解决 ,最后无意间发现

注册组件的单词敲错了啊 !!! 明明是 components , 我写成了 comments

只能说自己可真是瞎了眼啊 。。。


百度 Get 知识点 :

详细报错,可以看到页面渲染出来了




( 5 ) [Vue warn]:


Vue%20Router%204.%20Use%20the%20v-slot%20API%20to%20remove%20this%20warning%20%3A">一、[vue-router] <router-link>'s tag prop is deprecated and has been removed in Vue Router 4. Use the v-slot API to remove this warning :

https://router.vuejs.org/guide/migration/#removal-of-event-and-tag-props-in-router-link.

[vue路由器]<路由器链接>'s标记属性已弃用,并已在vue路由器4中删除。使用v-slot API删除此警告:

Migrating from Vue 2 | Vue Router

replace
<router-link to="/about" tag="span" event="dblclick">About Us</router-link>
with
<router-link to="/about" custom v-slot="{ navigate }">
  <span @click="navigate" @keypress.enter="navigate" role="link">About Us</span>
</router-link>
replace
<router-link active-class="back" to="/" tag="div">返 回</router-link>
with
<router-link to="/" custom v-slot="{ navigate }">
  <div class="back" @click="navigate" role="link">返 回</div>
</router-link>

解决方案二 :

replace
<router-link to="/" tag="div" active-class="back">返 回</router-link>
with
<div class="back" @click="goBack">返 回</div>

methods: {
  goBack() {
    this.$router.go(-1)
  }
}

二、[Violation] Addad non-passive event listener to a scroll-blocking 'mousewheel' event. Consider marking event handler as 'passive' to make the page more responsive.

See https://chromestatus.com/feature/5745543795965952

[冲突]将非被动事件侦听器添加到滚动阻止的“鼠标滚轮”事件。考虑将事件处理程序标记为“被动”,以使页面更具响应性。

See Passive event listeners - Chrome Platform Status


三、路由跳转错误:Uncaught (in promise) Error: Redirected when going from "/..." to "/..." via a navigation guard.

未捕获(承诺中)错误:从“/…”开始时重定向至“/…”通过导航卫士。

问题重现及产生原因

  1. 在项目开发中,并不是每一个路由跳转都是明确的。 例如很多页面跳转需要登录判断,如果你有登录,则跳转到指定页面,没有登录则跳转到登录页面。

项目代码:

  1. 项目中, 使用路由跳转页面, this.$router.push() 这个方法, 返回 Promise 对象, 如果不能正常跳转, 内部会抛出错误, 但是这个错误并不影响程序执行, 如果想解决控制台打印报错信息, 有 3 种方法

解决方式一: 投机取巧式

  • 在被路由导航守卫拦截后, 执行代码 console.clear() , 清空控制台的报错信息;
  • 注意: next() 方法是 异步函数, 需要在定时器中添加 console.clear() , 把这个操作添加进异步队列
router.beforeEach((to, from, next)=>{
  if(to.meta.isShow && !getToken()){
    next('/login')
    setTimeout('console.clear()', 300)  
    return
  }
  next()
})

解决方式二: 啰嗦式

// 使用编程式导航跳转时,每次使用,后面都跟上.catch方法,捕获错误信息
this.$router.push('/location').catch(err => ())

解决方式三: 高逼格式 (推荐)

  • 通过重写 VueRouter 原型对象上的 push 方法, 覆盖原来抛出异常的方法, "吞掉"异常
  • 切记: 一定要在 router 创建实例之前
import VueRouter from 'vue-router'

/* 在创建router实例对象之前,手动覆盖原型链的push来吞掉报错catch */
// 先存储默认底层的push
const originPush = VueRouter.prototype.push
// 覆盖原型链的push
VueRouter.prototype.push = function(location,resolve,reject){
    // this:路由实例对象
    
    // 判断用户有没有传后面两个可选参数
    if( resolve || reject ){
      return originPush.call(this,location,resolve,reject)
    }else{// 用户只传了第一个参数
      /* 
      默认底层: catch()方法代码  throw err : 抛出异常
      吞掉报错原理: 重写catch()方法,把默认底层的 throw err给去掉,就不会抛出异常
      */
      return originPush.call(this,location).catch(err => {
        //  throw err
      })
    }
}

const router = new VueRouter({
  routes
})

上述代码简化 :

import VueRouter from 'vue-router'

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, resolve, reject) {
    if ( resolve || reject ) return originalPush.call(this, location, resolve, reject)
    return originalPush.call(this, location).catch((e)=>{})
}

四、[WDS] Disconnected!

Vue 项目出现这个错误:[WDS] Disconnected! =>  [文字]已断开!

其实并没有对项目运行本身造成什么实质性的影响,

但是一条红色的提示摆在那里确实不太好看,还是把他给解决掉吧

在网上看好多人说

原因:
因为用了全局代理软件,所以需要将 config/index.js 中
{host:localhost} 改为
{host: 127.0.0.1} 刷新页面即可

但是我用起来并没有奏效,就另外找了一种解决方法:

打开 Application -> LocalStorage ,在 key 上添加 loglevel:webpack-dev-server ,

在 Value 上添加 SILENT 。

刷新页面以后错误就消失了。

( 这个是 webpack 的 热更新 配置 没搞好然后出来的问题,然后加的这个就不让它热更新了,

要么就是再改一下 , 去重新弄一下 webpack ) 


五、


http://www.niftyadmin.cn/n/1497917.html

相关文章

服务器操作系统应该选择Debian/Ubuntu还是CentOS?

任何 Linux 发行版本&#xff0c;在理论上都是一样的。只不过操作有的方便&#xff0c;有的麻烦&#xff01;yum 是比 apt 弱&#xff08;这就是企业维护和社区维护的区别&#xff0c;企业自己维护不需要这么多功能&#xff09;但是任何能在 A 发行版本上实现的效果&#xff0c…

raid5需要几块硬盘_什么是RAID 50? RAID 0的速度加上RAID 5的安全性

RAID(独立磁盘冗余阵列)是多个磁盘的组合&#xff0c;以模拟单个硬件存储设备的系统。根据组合类型&#xff0c;RAID以某种组合方式来提高性能、容量和容错能力。那么&#xff0c;什么是RAID 50&#xff1f;RAID 50跟RAID 5和RAID 0 有什么关系呢&#xff1f;RAID 5我们知道是以…

Linux下如何使用minicom USB串口

Linux下查看串口信息 查看串口是否可用&#xff0c;可以对串口发送数据比如对com1口&#xff0c;echo helloworld >/dev/ttyS0 查看串口名称使用 ls -l /dev/ttyS*一般情况下串口的名称全部在dev下面&#xff0c;如果你没有外插串口卡的话默认是dev下的ttyS*,一般ttyS0对应c…

Vuex _ 核心插件

目录 一、Vuex 1.1、概述 1.2、Vuex 核心概念 1.3、安装与配置 1.4、在组件中获取 Vuex 中的数据 1.5、moutations 同步修改 state 1.6、actions 配置 mutations 异步修改 state 数据 1.7、getters 1.8、modules 模块化 1.9、辅助函数 图例 : 我是因为啥才去用的 Vu…

python tkinter 按钮颜色_Python3 tkinter基础 Button bg 按钮的背景颜色

?python : 3.7.0OS : Ubuntu 18.04.1 LTSIDE : PyCharm 2018.2.4conda : 4.5.11type setting : Markdown?code"""Author : 行初心Date : 18-10-1Blog : www.cnblogs.com/xingchuxinGitHub : github.com/GratefulHeartCoder"""import tkinter a…

Android Studio 导入别人项目时候遇见的问题“Gradle DSL method not found: 'compile()'”

Gradle DSL method not found: compile() 遇见这个问题截图&#xff1a; 解决: 在项目的根目录的build.gradle文件中是不是用了compile方法 如果有的话&#xff0c;剪切&#xff0c;粘贴到app下build.gradle文件的dependencies中 重新构建一下项目解决 备忘 希望帮助到大家转载…

Vue _ 后台管理

目录 一、后台管理 1.1、elmentui 安装与配置 1.2、后台登录 1.2.1、全局混入实现自定义验证提取 1.2.2、局部混入实现自定义验证规则 1.2.3、登录按钮实现防抖处理 1.3、后台登录接口编写 1.3.1、jwt 1.3.2、生成 jwt 凭证 1.3.3、网络请求封装 1.4、权限控制 1.4…

递归从入门到精通

递归——初学者学习语言的最大障碍之一 递归是真的难懂&#xff0c;我每次都是绕进去&#xff0c;然后绕不出来&#xff0c;一个程序能做到这样&#xff0c;我服&#xff01;&#xff01;&#xff01; 我们看一段文字先欣赏一下递归的轮廓&#xff0c;如果看完不懂&#xff0c;…