uniapp ts onload不执行解决方法:组件配置与生命周期触发详解
@Component export default class IndexPage extends Vue { onLoad(options: any) {
console.log('页面参数', options) // 永远不会执行
} }
@Component({}) // 正确写法应该是 @Component({ components: {...} }) export class BugPage extends Vue { onLoad() { / 不会执行 / } }
@Component({ components: { // 必须显式声明才能激活生命周期
CustomComponent
} }) export class UserPage extends Vue { onLoad() { / 现在可以正常触发 / } }
// 插件核心检测逻辑 if (node.decorators?.find(d => d.expression.callee.name === 'Component')) { const hasOnLoad = node.body.body.some(m => m.key.name === 'onLoad'); if (!hasOnLoad) {
console.warn(`[生命周期警报] ${filename}缺少onLoad方法`);
} }