uniapp中使用selectComponent

selectComponent:selectComponent('.class') // 返回组件的实例
selectComponent获取的是在globalStyle或是pages每个页面中注册的组件实例

1:"globalStyle": {
		"navigationBarTextStyle": "black",
		"navigationBarTitleText": "uni-app",
		"navigationBarBackgroundColor": "#F8F8F8",
		"backgroundColor": "#F8F8F8",
		"usingComponents": {
			"wxml-to-canvas": "/wxcomponents/wxml-to-canvas/index",
			"content": "/pages/selectComponentApi/components/content/index"
		}
	},


2:"pages": [
		{
			"path": "pages/index/index",
			"style": {
				"navigationBarTitleText": "canvas",
                "usingComponents": {
			        "wxml-to-canvas": "/wxcomponents/wxml-to-canvas/index"
		        }
			}
		},
		
],
页面中获取组件实例

// html
<wxml-to-canvas
   class="widget"
   id="widget"
   :width="canvasWidth"
   :height="canvasHeight"
 />


// js 
onLoad() {
    this.init()
  },
  methods: {
    async init() {
      setTimeout(() => {
        this.widget = this.selectComponent(".widget") // 需要延迟
      }, 1000)
    },
  },

普通的组件引用无法用selectComponent获取组件实例

猜你喜欢

转载自blog.csdn.net/YZ_ZZZ24/article/details/127943055
今日推荐