const filteredMsgs = computed(() => {return this.checkFlag ? this.checkFlag.filter((t) => !t.done) : this.checkFlag})如何改正错误
时间: 2024-02-03 15:12:56 浏览: 34
这段代码错误的地方在于,在computed函数中使用了this关键字,而computed函数中的this指向的不是Vue实例。解决方法是将this改为箭头函数的参数,如下所示:
```
const filteredMsgs = computed((vm) => {
return vm.checkFlag ? vm.checkFlag.filter((t) => !t.done) : vm.checkFlag;
});
```
这样就可以正确地访问Vue实例的属性了。
相关问题
const shouldShowCloseButton = () => { return props.closable !== false && !props.tab?.disabled; } 在tsx里如何读取值
在tsx文件中,你可以这样读取`shouldShowCloseButton`函数的返回值:
```tsx
import React from 'react';
interface Props {
closable?: boolean;
tab?: {
disabled?: boolean;
};
}
const shouldShowCloseButton = (props: Props) => {
return props.closable !== false && !props.tab?.disabled;
};
export default function MyComponent(props: Props) {
const showCloseButton = shouldShowCloseButton(props);
return (
<div>
{showCloseButton && <button>Close</button>}
<p>Other content...</p>
</div>
);
}
```
在该例子中,我们定义了一个名为`Props`的接口来描述组件的props。我们定义了一个名为`shouldShowCloseButton`的函数,该函数接受一个`Props`类型的参数并返回一个布尔值。在组件中,我们调用`shouldShowCloseButton`函数并传入`props`参数,以获取`showCloseButton`的值。最后,我们可以根据`showCloseButton`的值来决定是否渲染关闭按钮。
const shouldShowCloseButton = () => { return props.closable !== false && !props.tab?.disabled; } 如何在tsx里写并且读取其值
可以在tsx文件中这样写:
```tsx
import React from 'react';
interface Props {
closable?: boolean;
tab?: {
disabled?: boolean;
};
}
const shouldShowCloseButton = (props: Props) => {
return props.closable !== false && !props.tab?.disabled;
}
export default function MyComponent(props: Props) {
const showCloseButton = shouldShowCloseButton(props);
return (
<div>
{showCloseButton && <button>Close</button>}
<p>Other content...</p>
</div>
);
}
```
在tsx文件中,我们可以直接定义一个名为`Props`的接口来描述组件的props。在函数组件中,我们可以通过`props`参数访问这些属性。同时,我们可以通过调用`shouldShowCloseButton`函数,并传入`props`参数,来获取`showCloseButton`的值。最后,我们可以根据`showCloseButton`的值来决定是否渲染关闭按钮。