1. 前言
2.1 什么是插件?
插件就可以理解为搭建房子的一块块砖头,别人已经帮我们烧制好了,我们直接垒上去就可以。
2.2 什么是插件开发?
3.1 module 模式
3.2 component 模式
比如调用某个地图厂商的 Map 组件,将地图组件插在页面中间,就需要把这个原生地图 SDK 封装为 Componet 模式。调用的时候与调用 vue 组件是差不多的,在 <template>
标签中写组件标签就可以了。
调用之前要在原生插件项目中先注册一下插件,具体位置在原生插件App 项目的 Module 根目录下,assets/dcloud_uniplugins.json文件中。在nativePlugins节点下添加要注册的 Module 插件或者 Component 插件。
实例:
{
"nativePlugins": [
{
"plugins": [
{
"type": "module",
"name": "ImoocModulePlugin",
"class": "TestModulePlugin"
}
]
},
{
"plugins": [
{
"type": "component",
"name": "ImoocComponentPlugin",
"class": "TestComponentPlugin"
}
]
}
]}
要注意 name 属性的设置,这个很重要,我们在 uni-app 框架中调用的时候,不管开发的时候插件用的什么名字,调用的时候,都要用 name 属性定义的名字来调用。下面我们来看看如何调用这些插件。
test:function(){
// 调用插件
const myTest = uni.requireNativePlugin('ImoocModulePlugin')
// 打印结果
console.log("获取结果:" + myTest.content)
}
<template>
<view>
<ImoocComponentPlugin style="width:;height:"></ImoocComponentPlugin>
</view>
</template>