现在代码是正常没问题运行的,但编辑器一直有这个提示
- 发布:2024-12-25 10:49
- 更新:2024-12-30 15:29
- 阅读:127
1***@qq.com (作者)
const compTemplate = computed(() => {
const templateId: number = 1
const imgUrlArr: string[] = [
'', 'image1', 'image2'
]
return {
imgUrl: new URL(`/static/${imgUrlArr[templateId]}/img_bg.png`, import.meta.url).href
}
})
-
1***@qq.com (作者)
回复 DCloud_HB_WJ: 你好,webUni变量的具体步骤我发在下面了,这里我不知道是不是我这种写法不对,这个变量是在js文件里面,然后index.html引入,再使用这个变量
2024-12-30 14:01
1***@qq.com (作者)
-
js文件放置这块代码
! function (e, n) { "object" == typeof exports && "undefined" != typeof module ? module.exports = n() : "function" == typeof define && define.amd ? define(n) : (e = e || self).webUni = n() }(this, (function () { "use strict"; try { var e = {}; Object.defineProperty(e, "passive", { get: function () { !0 } }), window.addEventListener("test-passive", null, e) } catch (e) {} var n = Object.prototype.hasOwnProperty; function t(e, t) { return n.call(e, t) } var i = [], a = function (e, n) { var t = { options: { timestamp: +new Date }, name: e, arg: n }; if (window.__dcloud_weex_postMessage || window.__dcloud_weex_) { if ("postMessage" === e) { var a = { data: [n] }; return window.__dcloud_weex_postMessage ? window.__dcloud_weex_postMessage(a) : window.__dcloud_weex_.postMessage(JSON.stringify(a)) } var o = { type: "WEB_INVOKE_APPSERVICE", args: { data: t, webviewIds: i } }; window.__dcloud_weex_postMessage ? window.__dcloud_weex_postMessageToService(o) : window.__dcloud_weex_.postMessageToService(JSON.stringify(o)) } if (!window.plus) return window.parent.postMessage({ type: "WEB_INVOKE_APPSERVICE", data: t, pageId: "" }, "*"); if (0 === i.length) { var r = plus.webview.currentWebview(); if (!r) throw new Error("plus.webview.currentWebview() is undefined"); var d = r.parent(), s = ""; s = d ? d.id : r.id, i.push(s) } if (plus.webview.getWebviewById("__uniapp__service")) plus.webview.postMessageToUniNView({ type: "WEB_INVOKE_APPSERVICE", args: { data: t, webviewIds: i } }, "__uniapp__service"); else { var w = JSON.stringify(t); plus.webview.getLaunchWebview().evalJS('UniPlusBridge.subscribeHandler("'.concat("WEB_INVOKE_APPSERVICE", '",').concat(w, ",").concat(JSON.stringify(i), ");")) } }, o = { navigateTo: function () { var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {}, n = e.url; a("navigateTo", { url: encodeURI(n) }) }, navigateBack: function () { var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {}, n = e.delta; a("navigateBack", { delta: parseInt(n) || 1 }) }, switchTab: function () { var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {}, n = e.url; a("switchTab", { url: encodeURI(n) }) }, reLaunch: function () { var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {}, n = e.url; a("reLaunch", { url: encodeURI(n) }) }, redirectTo: function () { var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {}, n = e.url; a("redirectTo", { url: encodeURI(n) }) }, getEnv: function (e) { window.plus ? e({ plus: !0 }) : e({ h5: !0 }) }, postMessage: function () { var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {}; a("postMessage", e.data || {}) } }, r = /uni-app/i.test(navigator.userAgent), d = /Html5Plus/i.test(navigator.userAgent), s = /complete|loaded|interactive/; var w = window.my && navigator.userAgent.indexOf("AlipayClient") > -1; var u = window.swan && window.swan.webView && /swan/i.test(navigator.userAgent); var c = window.qq && window.qq.miniProgram && /QQ/i.test(navigator.userAgent) && /miniProgram/i.test(navigator.userAgent); var g = window.tt && window.tt.miniProgram && /toutiaomicroapp/i.test(navigator.userAgent); var v = window.wx && window.wx.miniProgram && /micromessenger/i.test(navigator.userAgent) && /miniProgram/i.test(navigator.userAgent); var p = window.qa && /quickapp/i.test(navigator.userAgent); for (var l, _ = function () { window.UniAppJSBridge = !0, document.dispatchEvent(new CustomEvent("UniAppJSBridgeReady", { bubbles: !0, cancelable: !0 })) }, f = [function (e) { if (r || d) return window.__dcloud_weex_postMessage || window.__dcloud_weex_ ? document.addEventListener("DOMContentLoaded", e) : window.plus && s.test(document.readyState) ? setTimeout(e, 0) : document.addEventListener("plusready", e), o }, function (e) { if (v) return window.WeixinJSBridge && window.WeixinJSBridge.invoke ? setTimeout(e, 0) : document.addEventListener("WeixinJSBridgeReady", e), window.wx.miniProgram }, function (e) { if (c) return window.QQJSBridge && window.QQJSBridge.invoke ? setTimeout(e, 0) : document.addEventListener("QQJSBridgeReady", e), window.qq.miniProgram }, function (e) { if (w) { document.addEventListener("DOMContentLoaded", e); var n = window.my; return { navigateTo: n.navigateTo, navigateBack: n.navigateBack, switchTab: n.switchTab, reLaunch: n.reLaunch, redirectTo: n.redirectTo, postMessage: n.postMessage, getEnv: n.getEnv } } }, function (e) { if (u) return document.addEventListener("DOMContentLoaded", e), window.swan.webView }, function (e) { if (g) return document.addEventListener("DOMContentLoaded", e), window.tt.miniProgram }, function (e) { if (p) { window.QaJSBridge && window.QaJSBridge.invoke ? setTimeout(e, 0) : document.addEventListener("QaJSBridgeReady", e); var n = window.qa; return { navigateTo: n.navigateTo, navigateBack: n.navigateBack, switchTab: n.switchTab, reLaunch: n.reLaunch, redirectTo: n.redirectTo, postMessage: n.postMessage, getEnv: n.getEnv } } }, function (e) { return document.addEventListener("DOMContentLoaded", e), o }], m = 0; m < f.length && !(l = f[m](_)); m++); l || (l = {}); var E = "undefined" != typeof webUni ? webUni : {}; if (!E.navigateTo) for (var b in l) t(l, b) && (E[b] = l[b]); return E.webView = l, E }));
- index.html引入第1步的js文件
<script type="module" src="/util/webview.js"></script>
- 页面使用webview变量
const submitSign = () => { webUni.webView.getEnv((e: any) => { if (e.ios || e.android || e.plus) { webUni.webView.postMessage({ data: { action: 'realityLogin' } }) } }) } submitSign()
-
1***@qq.com (作者)
回复 DCloud_HB_WJ: 第3步那里不是html的文件,是vue的文件script里面使用,第2步html只引入了一些文件就没其它的了,完整的我重新发了下
2024-12-30 15:29
-
1***@qq.com (作者)
回复 DCloud_HB_WJ: 我刚刚试了下,如果加上// @ts-ignore 是不会提示划线了,那这里我是用这个忽略划线,还是那个变量用成什么样的写法会好一些
2024-12-30 15:33
1***@qq.com (作者)
- index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <script> var coverSupport = 'CSS' in window && typeof CSS.supports === 'function' && (CSS.supports('top: env(a)') || CSS.supports('top: constant(a)')) document.write( '<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0' + (coverSupport ? ', viewport-fit=cover' : '') + '" />') </script> <link rel="icon" href="/static/image/icon.ico"> <title></title> <!--preload-links--> <!--app-context--> </head> <body> <div id="app"><!--app-html--></div> <script type="module" src="/main.js"></script> <script type="module" src="/util/webview.js"></script> </body> </html>
- webview变量
<script lang="ts" setup> const submitSign = () => { webUni.webView.getEnv((e: any) => { if (e.ios || e.android || e.plus) { webUni.webView.postMessage({ data: { action: 'realityLogin' } }) } }) } submitSign() </script>
-
哦哦 我明白你的意思了,这里不推荐直接在html里引入js,然后在vue模板中使用这个js的变量。看你的使用场景好像确实只能使用// @ts-ignore。不过更好的方案应该是在vue模板中引入webview.js这个文件的内容,不过这个文件我看是第三方的吧,你估计也不好直接修改。
2024-12-30 15:38
-
1***@qq.com (作者)
回复 DCloud_HB_WJ: 这个文件好像是导出方式不兼容还是什么,直接import xxx from的方式就报错了,那我直接先使用// @ts-ignore了,谢谢大佬解答
2024-12-30 16:04
-
回复 1***@qq.com: 客气,如果你项目中有tsconfig.json,那你配置一下module为esnext也可以消除import.meta.url的问题,如果没有tsconfig.json就先使用// @ts-ignore,后续我们会修复这个bug。
2024-12-30 17:12
1***@qq.com (作者)
第一张图import.meta.url的那个提示代码我发了,就是动态路径图片引入的时候有提示那个,第二张就是index.html引入了webview文件里面的webUni变量,页面使用webUni变量就提示那个了
2024-12-27 16:33