在子组件中使用微信原生组件,通过在子组件内通过局部样式修改微信原生组件样式,发现在父组件中显示时修改的样式并不生效。
例如:

<style lang="scss">
	.info-page {
		/* switch */
		.wx-switch-input {
			width: 60upx !important;
			height: 30upx !important;
		}

		/*关闭状态*/
		.wx-switch-input::before {
			width: 30upx !important;
			height: 30upx !important;
			top: -2upx;
		}

		/*开启状态*/
		.wx-switch-input::after {
			width: 30upx !important;
			height: 30upx !important;
			top: -2upx;
		}
	}
</style>

但最终得到的switch样式并没有改变。
开始以为可能是因为组合样式的原因?结果去掉外层的info-page也一样
在这里插入图片描述

解决方案:
1、去掉info-page直接全局修改微信原生组件样式,放到全局样式代码中,但这种方式会有弊端,因为可能不同页面这个组件的样式并不一样
2、在子组件的vue对象中添加属性styleIsolation: ‘shared’

<script>
	export default {
		options: {
			styleIsolation: 'shared'
		},
		data() {
			return {

			}
		},
		methods: {

		}
	}
</script>

在这里插入图片描述

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐