問題描述:
在小程序創建自定義組件時,報錯: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"
}
}