vue踩坑:Property or method “form“ is not defined...Cannot read property ‘netName‘ of undefined...

本文详细解析了Vue.js开发中常见的错误,包括组件渲染失败、GET请求问题、数据格式不匹配及保存功能的多种错误场景,提供了逐一排查与修复的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

页面组件渲染不成功

(1)错误
在这里插入图片描述
(2)解决
这个错误真的排除了一遍又一遍,重新编译,修改变量名,删除变量名都会报这个错误,最后经过一个一个字母的核对,才发现自己是将data()给写成了date()…

<script>
	export default {
		name: "LAN",
		date(){
			return{
				form:{
					netName: '',
					dhcp: false,

修改

<script>
	export default {
		name: "LAN",
		data(){
			return{
				form:{
					netName: '',
					dhcp: false,

get请求时报错(1)

(1)错误:get请求不成功
在这里插入图片描述
(2)解决
在挂载的钩子函数里面调用函数:

mounted() {
	this.$fun.GET();
	console.log(this.form);
},

此处调用是从fun.js文件中调用这个函数,在这里也不知道为什么会行不通了,因此我在这个vue文件里重新声明了GET函数:

mounted() {
	this.GET();
	console.log(this.form);
},
methods: {
	GET(){
		this.$axios.postJson(this.$urls.API,{
			session: Lockr.get(this.$constant.SESSION),
			call:{
				service: this.$constant.SYSTEM,
				method: this.$urls.GETLAN
			},
			params:{}
		}).then((res) => {
			if(res.code === 0){
				this.form = res.data;
			}
		});
	}
}

get请求时报错(2)

(1)错误:get数据时,仅显示网卡名称
在这里插入图片描述
(2)解决
在这里插入图片描述
在response中可以发现,数组返回时已经正确,但是无法传递到前端页面,说明前端数据格式定义存在问题,例如form.mac,修改为form.ipv4.mac,这是前后端分离而数据格式不匹配所带来的错误

保存时报错(1)

(1)错误
点击保存以后,有数据但无法上传,提示如下:
在这里插入图片描述

(2)解决

rules:{
	ip: [{required: true, message: this.$t('ip can not null'),trigger: 'blur'}],
	netmask: [{required: true, message: this.$t('netmask can not null'),trigger: 'blur'}],
	gateway: [{required: true, message: this.$t('gateway can not null'),trigger: 'blur'}],
	dnsPrimary: [{required: true, message: this.$t('dnsPrimary can not null'),trigger: 'blur'}],
	dnsSecond: [{required: true, message: this.$t('dnsSecond can not null'),trigger: 'blur'}],
}

修改为:

rules:{
	ipv4:{
		ip: [{required: true, message: this.$t('ip can not null'),trigger: 'blur'}],
		netmask: [{required: true, message: this.$t('netmask can not null'),trigger: 'blur'}],
		gateway: [{required: true, message: this.$t('gateway can not null'),trigger: 'blur'}],
		dnsPrimary: [{required: true, message: this.$t('dnsPrimary can not null'),trigger: 'blur'}],
		dnsSecond: [{required: true, message: this.$t('dnsSecond can not null'),trigger: 'blur'}],
	}
}

保存时报错(2)

(1)问题
vue.runtime.esm.js?0261:619 [Vue warn]: Error in v-on handler: “ReferenceError: API is not defined”
在这里插入图片描述
(2)解决
由错误分析可知该处错误出在API处,并且这是在点击保存按钮后报的错误,因此,我们去仔细查看自己关于保存相关的包含API的函数代码的准确性

SET(){
	this.$axios.postJson(this.$urls,API,{
		session: Lockr.get(this.$constant.SESSION),
		call:{
			service: this.$constant.SYSTEM,
			method: this.$urls.SETLAN
		},
		params:{},
			}).then((res) => {
				if (res.code === 0){
					this.$notify({
					title: this.$t(constant.SUCCESS),
					message: this.$t(constant.SAVESCCESS),
					type: 'success'
					})
				}
			})
	}

可以看到,这里错误是因为API前用的是,而不是.!!!这种错误不会在编译器里报错,所以会十分隐蔽!

SET(){
	this.$axios.postJson(this.$urls.API,{
		session: Lockr.get(this.$constant.SESSION),
		call:{
			service: this.$constant.SYSTEM,
			method: this.$urls.SETLAN
		},
		params:{},
			}).then((res) => {
				if (res.code === 0){
					this.$notify({
					title: this.$t(constant.SUCCESS),
					message: this.$t(constant.SAVESCCESS),
					type: 'success'
					})
				}
			})
	}

保存时报错(3)

(1)错误:
在这里插入图片描述
(2)解决
在Network的Headers中可以查看到,此时的save请求是已经成功的,对与setLAN接口也能成功调用,那么我们所报的错误并不是在于调用函数之中
在这里插入图片描述
这时候再去看自己SET函数的代码:

SET(){
		this.$axios.postJson(this.$urls.API,{
			session: Lockr.get(this.$constant.SESSION),
			call:{
				service: this.$constant.SYSTEM,
				method: this.$urls.SETLAN
			},
			params:{},
		}).then((res) => {
			if (res.code === 0){
				this.$notify({
					title: this.$t(constant.SUCCESS),
					message: this.$t(constant.SAVESCCESS),
					type: 'success'
				})
			}
		})
	},
	save(form){
		if (this.validateForm(form)) {
			this.SET(this.$constant.SYSTEM,this.$urls.SETLAN,this.form)
		}
	},
	validateForm(form){
		let count = 0;
		this.$refs[form].validate((valid) => {
			if (!valid) {
				count++;
			}
		});
		return count === 0;
	}

出问题最大的可能就在于notify这个地方

if (res.code === 0){
	this.$notify({
		title: this.$t('success'),
		message: this.$t('save success'),
		type: 'success'
	})
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值