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获取组件实例

Logo

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

更多推荐