uniapp项目代码
<template>
<view class="content">
<image class="logo" src="/static/logo.png" @click="onMuTest"></image>
<view class="text-area">
<text class="title">{{title}}</text>
</view>
</view>
</template>
<script>
let muTest;
export default {
data() {
return {
title: 'Hello'
}
},
onLoad() {
muTest = uni.requireNativePlugin('Mu-Test')
this.title = !muTest ? '插件导入失败' : '插件导入成功'
},
methods: {
onMuTest() {
uni.showToast({
title: '点击了',
icon: 'none'
});
if(muTest) {
muTest.test({
code: 1
}, res => {
this.title = JSON.stringify(res)
})
}
}
}
}
</script>
<style>
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.logo {
height: 200rpx;
width: 200rpx;
margin-top: 200rpx;
margin-left: auto;
margin-right: auto;
margin-bottom: 50rpx;
}
.text-area {
display: flex;
justify-content: center;
}
.title {
font-size: 36rpx;
color: #8f8f94;
}
</style>
插件module代码
package mu.test;
import com.alibaba.fastjson.JSONObject;
import io.dcloud.feature.uniapp.annotation.UniJSMethod;
import io.dcloud.feature.uniapp.bridge.UniJSCallback;
/**
* @author LiYejun
* @date 2021/12/9 10:01
*/
public class TestModule {
@UniJSMethod()
public void test(JSONObject options, UniJSCallback callback) {
if(callback != null) {
JSONObject data = new JSONObject();
data.put("code", "success");
data.put("data", options);
callback.invoke(data);
}
}
}
插件配置dcloud_uniplugins.json
{
"nativePlugins": [
{
"hooksClass": "",
"plugins": [
{
"type": "module",
"name": "Mu-Test",
"class": "mu.test.TestModule"
}
]
}
]
}
mu0918 (作者)
确实混淆问题
2021-12-10 14:40