computed: { histories() { //展开运算符,基于原来的 historyList,在调用reverse方法 return [...this.historyList].reverse() } }为什么会报错
时间: 2023-06-20 11:10:32 浏览: 64
可以提供一下报错的具体信息吗?不过根据你提供的代码片段,可能是因为computed中的histories没有使用箭头函数,并且在函数内部使用了this关键字。在Vue中,computed属性中的函数会自动绑定到组件实例上。如果使用普通函数,this指向的就不是组件实例,而是window对象,所以会报错。建议将computed中的函数改为箭头函数,代码如下:
```
computed: {
histories: () => [...this.historyList].reverse()
}
```
这样this就可以正确指向组件实例。
相关问题
解释一下: computed: { unreadNum() { return this.unread.length; }, },
这段代码是一个计算属性对象,它的名称为`unreadNum`,它没有参数。该计算属性对象的作用是计算未读消息数量。
具体实现过程是,通过`return`关键字返回`this.unread.length`的值。`this.unread`是一个数组,存储了从服务器获取到的未读消息列表。因此,`this.unread.length`表示未读消息的数量。
当未读消息列表中的数据发生变化时,`unreadNum`也会自动更新,因此可以在组件模板中直接使用`unreadNum`计算属性,而不需要手动更新未读消息数量。
computed: { onRoutes() { return this.$route.path.replace('/', ''); } },
这段代码定义了一个计算属性 onRoutes,它返回 this.$route.path.replace('/', ''),即当前路由的路径去掉开头的‘/’后的字符串。这里使用了 Vue.js 的路由功能,$route 是 Vue.js 自带的一个对象,包含当前路由的相关信息,如路径、参数等。computed 属性用于定义计算属性,它会根据依赖的响应式数据自动计算出一个新的值。在这里,onRoutes 计算属性依赖于 $route.path,当 $route.path 发生变化时,Vue.js 会自动重新计算 onRoutes 的值。
相关推荐
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)