问题描述:
在小程序创建自定义组件时,报错:thirdScriptError sdk uncaught third Error Cannot read property ‘name’ of undefined
解决方法:
最后发现可能是properties变量的问题,按照官方文档https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/将properties改成了:
Component({
properties: {
// 这里定义了innerText属性,属性值可以在组件使用时指定
innerText: {
type: String,
value: 'default value',
}
},
data: {
// 这里是一些组件内部数据
someData: {}
},
methods: {
// 这里是一个自定义方法
customMethod: function(){}
}
})
上面的innerText就是你的变量,如果你想增加一个名叫abc的变量,也只需要在properties变量中增加:
properties: {
// 这里定义了innerText属性,属性值可以在组件使用时指定
innerText: {
type: String,
value: 'default value',
},
abc: {
type: String,
value: 'default value',
}
},
其实可以去掉默认值,更加简化成:
properties: {
year: Number,
month: Number,
type: String,
},
**
创建自定义组件其他需要注意的地方:
**
1.json文件中要声明自己是自定义组件:
{
"component": true
}
2.使用自定义组件前要进行声明:
首先要在页面的 json 文件中进行引用声明。此时需要提供每个自定义组件的标签名和对应的自定义组件文件路径:
{
"usingComponents": {
"component-tag-name": "path/to/the/custom/component"
}
}