Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript TypeError:this.decodeHtml不是函数_Javascript_Vue.js - Fatal编程技术网

Javascript TypeError:this.decodeHtml不是函数

Javascript TypeError:this.decodeHtml不是函数,javascript,vue.js,Javascript,Vue.js,我刚刚迁移到VueJS 2,现在,我遇到了一个问题 在筛选器中,我调用了一个自定义函数,但得到:TypeError:this.decodeHtml不是一个函数 这是我的密码: new Vue({ el: '#modal', data: {...} computed: {...} methods: { decodeHtml: function decodeHtml(html) {

我刚刚迁移到VueJS 2,现在,我遇到了一个问题

在筛选器中,我调用了一个自定义函数,但得到:
TypeError:this.decodeHtml不是一个函数

这是我的密码:

 new Vue({
         el: '#modal',
         data: {...}
         computed: {...}
         methods: {
                decodeHtml: function decodeHtml(html) {
                        var txt = document.createElement("textarea");
                        txt.innerHTML = html;
                        return txt.value;
                    },
         ... },
        filters: {
                    html: function html(_html) {
                        return this.decodeHtml(_html);
                    }
                }
在我的HTML中,我称之为:
@{{categoryFullName | HTML}}

知道为什么吗???在迁移之前,它工作得很好


这是您在范围方面遇到问题的原因。在
html
函数中,
变量是
窗口
对象,而不是
Vue
对象

您可以做的变通方法是不调用在过滤器中实现的
decodeHtml
函数。像这样:

filters: {
  html: (html) => {
    var txt = document.createElement("textarea");
    txt.innerHTML = html;
    return txt.value;
  }
}

console.log(this)
的结果是什么?为什么要用关键字
function
来命名函数?我应该如何命名?在您的例子中:
decodeHtml:function(){…}
。但我可能错了,这也是命名函数的有效方法。它给了我一个窗口对象。我在我的问题中添加了调用(这显然是一个范围问题),切换到箭头函数来定义函数(两者)。看看有没有用。所以,“decodeHTML:(html)=>{……return等},和html:(html)=>{return等}”