vue.js javascript 前端 【Vue】常见错误总结 - $refs[‘form’]

📅 2026-12-01 👤 拼多多助力营销技巧 📂 拼多多助力平台 👁️ 112 次阅读

目录

this.$refs[‘form’].resetFields()

this.defParams = {...this.params}

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

数据还没获取就已渲染页面

页面跳转后没有重新请求接口

a、updated刷新组件内的方法更新数据

b、监听路由去请求方法更新数据

调用子组件提示this.$refs["xxxRef"].xx() underfined

a、首次渲染不调用

b、加载完数据再请求

Vue项目dialog内组件数据刷新总是延迟慢一拍

解决 npm ERR A complete log of this run can be found in: npm ERR

Error in callback for watcher "$route": "TypeError: this.$refs.xxxRef is undefined"

this.$refs[‘form’].resetFields()

要想this.$refs[‘form’].resetFields()方法有效,必须配置el-form :model 属性和el-form-item中的prop属性才可以

this.$refs[‘form’].resetFields()这个做法其实是重置表单到初始值,不是清空表单,当表单第一次在页面中渲染时所用的数据就是初始数据,

this.defParams = {...this.params}

this.params=this.defParam;初始化默认参数, vue2会把内存地址也复制过去,当params里面的值修改后, this.defParams也会相应改动,所以要先复制一份再赋值给this.params

this.defParams = {...this.params},

这样它们之间的值才不会相互影响, 不过这在Vue3里面都已经解决了

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

改动了子组件中引用的父组件的变量,也就是props中的数据

中组件props中的数据只能单向流动,即只能从父组件通过组件的DOM属性attribute传递props给子组件,子组件只能被动接收父组件传递过来的数据,并且在子组件中,不能修改由父组件传来的props数据。

props:{

cid:{

type:String,

},

},

methods: {

//父组件刷新子组件数据1

flush(cid){

const params = {cid:cid};

this.getData(params);

},

//父组件刷新子组件数据2, 这种情况就会出现上述问题

flush(cid){

this.cid= cid;

const params = {cid:this.cid};

this.getData(params);

},

数据还没获取就已渲染页面

简单举例

...

v-if="loading"

...

data(){

return{

loading:false,

}

}

created(){

this.init();

},

methods: {

init() {

this.getList();

},

getList() {

queryListData(this.params)

.then((res) => {

if(res.code==='200'){

...

this.loading = true;

}

})

.finally(() => {

... 拼多多帮助力入口

});

},

}

页面跳转后没有重新请求接口

a、updated刷新组件内的方法更新数据

简单举例

...

ref="demoRef"

...

created() {},

mounted() {},

updated() {

this.init();

},

methods: {

init() {

this.$refs["demoRef"].flush();

}

},

}

b、监听路由去请求方法更新数据

简单举例

...

ref="demoRef"

...

created() {},

mounted() {},

updated() {},

watch: {

$route(a, b) {

if (a.path != b.path) {

this.init();

}

},

//-----或-----

$route(-) {

this.init();

},

},

methods: {

init() {

this.detail();

...

}

},

}

调用子组件提示this.$refs["xxxRef"].xx() underfined

a、首次渲染不调用

简单举例

...

ref="demoRef"

...

data() {

return {

isFirst:true,

},

}

created() {

this.detail();

},

mounted() {},

updated() {},

methods: {

detail() {

if(!this.isFirst){

//首次渲染不调用

this.$refs["demoRef"].flush();

}

this.isFirst=false;

}

},

}

b、加载完数据再请求

简单举例

...

ref="demoRef"

...

data() {

return {

loadingData:false,

},

}

created() {

this.init();

},

mounted() {},

updated() {

if(loadingData){

this.$refs["demoRef"].flush();

}

},

methods: {

init() {

queryListData(this.params)

.then((res) => {

...

this.loadingData= true;

});

}

},

}

【精选】【Vue】created、mounted、updated详解_vue created-CSDN博客

Vue项目dialog内组件数据刷新总是延迟慢一拍

Vue项目dialog内组件数据刷新总是延迟慢一拍_visible.sync点击取消有延迟_慕白Lee的博客-CSDN博客

解决 npm ERR A complete log of this run can be found in: npm ERR

【解决】npm ERR A complete log of this run can be found in: npm ERR_npm err! unexpected token '.' npm err! a complete -CSDN博客

Error in callback for watcher "$route": "TypeError: this.$refs.xxxRef is undefined"

父页面监控路由变动时刷新子组件提示, 这里是同一个页面路由参数改变,

Error in callback for watcher "$route": "TypeError: this.$refs.tableOneRef is undefined"

困扰许久, this.nextTick(){}等等都不好使, 原因是在Vue中,当路由发生变化时,组件的实例会被销毁然后重新创建。因此,在路由切换后,组件的this上下文会发生改变。

当你监听路由变化时,如果在回调函数中使用了this关键字,它将指向新创建的组件实例,而不是之前的组件实例。这意味着你无法直接访问之前组件实例中的数据和方法。

为了解决这个问题,你可以在监听路由变化时,将需要保留的数据和方法保存到其他变量中,或者使用Vue提供的beforeRouteUpdate导航守卫来处理组件的更新逻辑。

beforeRouteUpdate(to, from, next) {

// 保存需要保留的数据和方法

const preservedData = this.data;

const preservedMethod = this.method;

// 执行路由变化前的逻辑

// ...

// 调用next()继续路由更新

next();

// 在路由更新后恢复数据和方法

this.data = preservedData;

this.method = preservedMethod;

}

依然不好使哈

这里说下最后的解决思路:

定义一个状态isReloadChild:true

监控路由变化时, 先false, 然后更改状态为true, 需要控制的组件v-if控制

简化代码如下

this.isReloadChild = false;

setTimeout(() => {

this.isReloadChild = true;

}, 0);

有用请点赞,养成良好习惯!

疑问、交流、鼓励请留言!

精彩链接

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧