小程序开发错误:不允许的选择器 - 标签名、ID 和 属性选择器
小程序开发中,经常会遇到“Some selectors are not allowed in component wxss”的错误提示,比如“'[pages/index/index] Some selectors are not allowed in component wxss, including tag name selectors, ID selectors, and attribute selectors. (./node-modules/uview-ui/components/u-swiper-indicator/u-swiper-indicator.wxss:30:1)'”。这个错误提示表明在组件的 wxss 文件中使用了不允许的选择器,包括标签名选择器、ID 选择器和属性选择器。
小程序限制了组件样式的作用域,只允许使用类选择器和后代选择器来限定样式的范围。所以,尽量避免在组件的 wxss 文件中使用不允许的选择器。
如果确实需要使用特定的选择器,可以考虑将样式写在页面的 wxss 文件中,然后通过 class 或自定义属性将样式应用到组件中。
例如:
// 页面 wxss 文件.my-swiper-indicator { /* 样式代码 */}
// 组件 wxml 文件
这样可以确保组件样式的正确应用,同时避免出现错误。
原文地址: https://www.cveoy.top/t/topic/n8t4 著作权归作者所有。请勿转载和采集!