js方法如下: saveInsertBoardBodyType(){ var newBoardBodyType = {type:this.createBoardType,price:this.createBoardPrice} this.boardTypeBody.push(newBoardBodyType); localStorage.setItem('boardBodyType',JSON.stringify(this.boardTypeBody)); this.closeInsertBoardBodyType(); } ; <body>中的相关代码如下: <label>新增板材类型名称:</label> <input type="text" placeholder="请输入类型名称" v-model="this.createBoardType"> <label>新增板材价格</label> <input type="number" placeholder="请输入价格" v-model="this.createBoardPrice"> <button type="button" class="btn btn-link" @click="saveInsertBoardBodyType()">保存</button>, 帮我检查一下, 在点击saveInsertBoardBodyType()方法后报错:" Cannot read properties of null (reading 'push')"
时间: 2024-01-16 21:03:01 浏览: 23
这个错误通常会出现在 `this.boardTypeBody` 变量没有被正确初始化的情况下。你需要在 `data` 选项中定义 `boardTypeBody` 变量,并给它一个空数组作为默认值,例如:
```
data() {
return {
boardTypeBody: []
}
}
```
这样,在 `saveInsertBoardBodyType()` 方法中调用 `push()` 方法时,`boardTypeBody` 数组就已经存在了,就不会再出现 "Cannot read properties of null (reading 'push')" 的错误了。
另外,在 `v-model` 绑定中不需要使用 `this`,应该直接使用 `createBoardType` 和 `createBoardPrice` 变量名,例如:
```
<input type="text" placeholder="请输入类型名称" v-model="createBoardType">
<input type="number" placeholder="请输入价格" v-model="createBoardPrice">
```
这样也可以避免一些潜在的问题。
相关问题
var tr = this.parentNode.parentNode; tr.childElement.remove();
这段代码有语法错误,应该将 `tr.childElement.remove()` 修改为 `tr.remove()`。因为 `tr` 变量已经表示了要删除的节点,可以直接调用 `remove()` 方法删除该节点,不需要再通过 `childElement` 属性获取子节点。
因此,正确的代码应该是:
```javascript
var tr = this.parentNode.parentNode;
tr.remove();
```
这段代码的作用是,通过 `this` 获取当前按钮元素节点,并通过 `parentNode` 属性获取该节点的父节点,即要删除的表格行元素节点。然后调用 `remove()` 方法从 DOM 树中永久性删除该节点。
register () { var _this = this if (this.registerForm.password !== this.registerForm.passwordConfirm) { this.$message({ message: '两次输入的密码不一致', type: 'error'
缺少了 register 方法的右括号,建议补上,完整的代码如下:
```
register () {
var _this = this
if (this.registerForm.password !== this.registerForm.passwordConfirm) {
this.$message({
message: '两次输入的密码不一致',
type: 'error'
})
return
}
// ...其他代码
}
```
此处代码的作用是在注册时判断两次输入的密码是否一致,若不一致则给出提示并停止执行后续代码。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)