我有一个用例,要求我使用窗口级普通 JavaScript 函数才能刷新/更新已渲染的 Vue 应用程序内的数据。我无法真正使用 Vue 本身内部的任何异步进程或触发器,因为本质上这个应用程序将在类似于 iframe 的内部运行。
我遵循了 Vue 的入门指南,虽然我能够使用默认数据渲染我的简单表格,但从 Vue 外部调用时我无法让函数正常工作。我使用 tailwind 和 vite-single-file 将其编译成单页代码文件(所有这些都有效)。
我期望做的是运行一个函数在窗口中传递一些 JSON,***refreshData(json) ***,然后让 App.vue 中的 PerformRefresh 函数根据提供的数据更新模型数据。
我尝试过的代码看起来很简单:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test App</title>
<script>
function refreshData(newData) {
app.performRefresh(newData);
}
</script>
</head>
<body class="m-0 p-0">
<div id="app" class="w-full"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
import './assets/main.css'
import { createApp } from 'vue'
import App from './App.vue'
const app = createApp(App)
window.app = app;
app.mount('#app')
<script setup lang="ts">
import { ref } from 'vue'
import data from './assets/defaultData.json'
const tableData = ref(data)
const performRefresh = (newData) => {
console.log("Test Reactivity Function");
tableData.test = newData;
}
window.performRefresh = performRefresh;
</script>
<template>
<div class="px-4">
<table class="w-full">
<tr v-for="row in tableData.test">
# Standard model rendering with mustaches here
</tr>
</table>
</div>
<h1>TEST VALUE: {{ tableData.test }}</h1>
</template>
我尝试了多种不同的方法,但都以不同的失败结果告终。主要来自阅读 StackOverflow、Vue3 文档和在线其他类似主题的建议。
我觉得我在这里遗漏了一些基本和明显的东西,但无法完全连接这些部分。感谢您花时间向 Vue 新手用户提出建议。
您可以公开该方法(在Composition API中使用
defineExpose()
):
<script setup lang="ts">
import { defineExpose } from 'vue'
const performRefresh = (newData) => {...}
defineExpose({performRefresh})
</script>
然后你可以通过
app
变量访问它:
app._instance.exposed.performRefresh(newData)
这是一个片段:
const { createApp, ref } = Vue;
const App = {
template: `<div :style="{background: color, padding: '12px'}">colored div in Vue</div>`,
expose: ['setColor'],
setup(){
const color = ref('yellow')
const setColor = (colorString) => color.value = colorString
return {color, setColor}
},
}
window.app = createApp(App)
app.mount('#app')
window.updateColor = function(){
const randomColor = '#' + Math.floor(Math.random()*16777215).toString(16);
app._instance.exposed.setColor(randomColor)
}
<div id="app"></div>
<button onclick="updateColor()">tell Vue to update color</button>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>