我是TypeScript的新手。以下代码可以在JavaScript上正常运行,但此处未编译。
export default {
data: function() {
return {
data: [
'Angular',
'Angular 2',
'Aurelia',
'Backbone',
'Ember',
'jQuery',
'Meteor',
'Node.js',
'Polymer',
'React',
'RxJS',
'Vue.js'
],
name: "",
selected: null,
hasVariationRadio: ""
};
},
computed: {
filteredDataArray(): [] {
return this.data.filter((option: string) => { // This is where the error appears
return option
.toString()
.toLowerCase()
.indexOf(this.name.toLowerCase()) >= 0 // Also here.
})
}
}
};
我遇到以下错误:
Property 'name' does not exist on type '{ filteredDataArray(): []; }'.
如here所述,strict
文件中的compileOptions内部的true
设置为tsconfig.json
。
答案 0 :(得分:0)
当我在控制台中运行此代码时,它似乎根本不正确。
当您在this
中引用computed.filteredDataArray
时,this
是computed
对象,而不是整个对象。
将filteredDataArray
移至更高一级可以使其起作用:
export default {
data: function () {
return {
data: [
'Angular',
'Angular 2',
'Aurelia',
'Backbone',
'Ember',
'jQuery',
'Meteor',
'Node.js',
'Polymer',
'React',
'RxJS',
'Vue.js'
],
name: "",
selected: null,
hasVariationRadio: ""
};
},
filteredDataArray() {
return this.data().data.filter((option: string) => {
return option
.toString()
.toLowerCase()
.indexOf(this.data().name.toLowerCase()) >= 0
})
}
};